Immutable Remove With The Spread Operator
ES6 introduces the spread operator which allows you to expand arrays in place for function calls, array composition, array destructuring, etc. One thing the spread operator allows you to concisely do with array composition is perform immutable operations on arrays. For instance, to remove an item from an array by index, you can throw together the following function.
It only took a couple lines of code and immutability is baked in.
There may be a couple edge cases that are not handled in this solution (e.g. remove(list, -1)
), but you get the general idea.
Last updated