Identifies the type of array elements
A new array with the element at index replaced, or the original array if index is out of range
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"]
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.