@nevware21/ts-utils
    Preparing search index...

    Function arrWith

    • The arrWith() method is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value. This is an ES2023 feature with polyfill support for older environments.

      Type Parameters

      • T

        Identifies the type of array elements

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to copy from

      • index: number

        The index at which to change the value. Negative index counts from the end

      • value: T

        The new value to set at the index

      Returns T[]

      A new array with the element at index replaced, or the original array if index is out of range

      0.14.0

      arrWith([1, 2, 3], 1, 5);       // [1, 5, 3]
      arrWith([1, 2, 3], -1, 5); // [1, 2, 5]
      arrWith([1, 2, 3], -2, 5); // [1, 5, 3]
      arrWith([1, 2, 3], 10, 5); // RangeError (out of bounds)

      const original = [1, 2, 3];
      const modified = arrWith(original, 1, 9);
      // original: [1, 2, 3], modified: [1, 9, 3]

      // Array-like objects
      arrWith({ length: 3, 0: "a", 1: "b", 2: "c" }, 1, "x"); // ["a", "x", "c"]