What is the cleanest way to remove an element from an immutable array in JS? [duplicate]
You can use a combination of spread and Array#slice:
const arr = ['a', 'b', 'c', 'd', 'e'];const indexToRemove = 2; // the 'c'const result = [...arr.slice(0, indexToRemove), ...arr.slice(indexToRemove + 1)];console.log(result);
Another option is Array#filter:
const arr = ['a', 'b', 'c', 'd', 'e'];const indexToRemove = 2; // the 'c'const result = arr.filter((_, i) => i !== indexToRemove);console.log(result);