Return a copy of an array with one index replaced immutably
State libraries such as Redux treat arrays as immutable: to change one slot, you build a new array. Implement replaceAt(array, index, value). It returns a new array of the same length in which the element at index is value and every other element is copied from array. The input array is never changed.
replaceAt(array, index, value) // => a new array
index is truncated toward zero: 1.8 becomes 1 and -1.5 becomes -1. NaN is treated as 0.-1 is the last element, -array.length is the first.0 or not less than array.length, throw a RangeError. This includes any index on an empty array and Infinity.[1, , 3]) becomes an own slot holding undefined in the copy.value equals the current element.const scores = [70, 85, 92];
replaceAt(scores, 1, 88);
// => [70, 88, 92]
scores;
// => [70, 85, 92] (unchanged)
replaceAt(['draft', 'review', 'live'], -1, 'archived');
// => ['draft', 'review', 'archived']
replaceAt(['x', 'y', 'z'], 1.9, 'Y');
// => ['x', 'Y', 'z']
replaceAt([1, , 3], 0, 9);
// => [9, undefined, 3] (index 1 is a real slot now)
replaceAt([1, 2, 3], 3, 0);
// throws RangeError
replaceAt([1, 2, 3], -4, 0);
// throws RangeError
Do not use Array.prototype.with or Array.prototype.toSpliced.