How can the following operation be done without mutating the array:
let array = [\'item1\'];
console.log(array); // [\'item1\']
array[2] = \'item2\'; // array is
You can simply set up a a new array as such:
const newItemArray = array.slice();
And then set value for the index which you wish to have a value for.
newItemArray[position] = newItem
and return that. The values under the indexes in-between will have undefined
.
Or the obviously alternative would be:
Object.assign([], array, {: newItem});