Replaces adjacent items near (or including) a specified value in the array
npm install @writetome51/array-replace-adjacent-to-valueReplaces howMany adjacent items in array with newValues, starting at, or
close to, value. Exactly where the replacing begins is decided by offset,
which is the position, relative to value, where to begin replacing.
For example, if offset is 0, then replacing begins at value. If -1,
it begins one place to the left of value. If 1, it begins one place to the
right.
Note: the function only works with the first found instance of value.
let arr = [1, 2, 3, 4, 5, 6, 7];
replaceAdjacentToValue(
{value: 3, howMany: 2, offset: 1},
[8],
arr
);
// arr is now [ 1, 2, 3, 8, 6, 7 ]arr = [1, 2, 3, 4, 5, 6, 7];
replaceAdjacentToValue(
{value: 3, howMany: 3, offset: 0},
[8, 9],
arr
);
// arr is now [ 1, 2, 8, 9, 6, 7 ]
arr = [1, 2, 3, 4, 5, 6, 7, 5];
replaceAdjacentToValue(
{value: 5, howMany: 1, offset: -2},
[10, 20],
arr
);
// arr is now [ 1, 2, 10, 20, 4, 5, 6, 7, 5 ]
`$3
npm i @writetome51/array-replace-adjacent-to-value$3
`
// if using TypeScript:
import {replaceAdjacentToValue} from '@writetome51/array-replace-adjacent-to-value';
// if using ES5 JavaScript:
var replaceAdjacentToValue =
require('@writetome51/array-replace-adjacent-to-value').replaceAdjacentToValue;
``