Skip to content

adjust

The adjust function applies a function to an element at a specific index in an array, returning a new array with the adjusted value.

Syntax

typescript
adjust<T>(array: T[], index: number, fn: (value: T) => T): T[];

Parameters

NameTypeDescription
arrayT[]Array to be adjusted
indexnumberIndex of the element to be adjusted
fn(value: T) => TFunction to apply to the element

Returns

TypeDescription
T[]New array with the adjusted element

Examples

typescript
adjust([1, 2, 3], 1, x => x * 10); // => [1, 20, 3]
adjust([1, 2, 3], -1, x => x + 1); // => [1, 2, 4]

Notes

  • The index can be negative to count from the end of the array.
  • Does not modify the original array.

References

Released under the MIT License.