diff --git a/README.md b/README.md index 2f7e9f5..5ff494c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_362 TILs and counting..._ +_363 TILs and counting..._ --- @@ -121,6 +121,7 @@ _362 TILs and counting..._ - [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md) - [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md) - [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md) +- [Immutable Remove With The Spread Operator](javascript/immutable-remove-with-the-spread-operator.md) - [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md) - [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md) - [Throttling A Function Call](javascript/throttling-a-function-call.md) diff --git a/javascript/immutable-remove-with-the-spread-operator.md b/javascript/immutable-remove-with-the-spread-operator.md new file mode 100644 index 0000000..79c1a56 --- /dev/null +++ b/javascript/immutable-remove-with-the-spread-operator.md @@ -0,0 +1,24 @@ +# Immutable Remove With The Spread Operator + +ES6 introduces the [spread +operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/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. + +```javascript +const remove = (items,index) => { + return [...items.slice(0,index), + ...items.slice(index+1,items.length)]; +}; + +const list = [1,2,3,4,5]; +remove(list, 2); +// [1,2,3,4] +list +// [1,2,3,4,5] +``` + +It only took a couple lines of code and immutability is baked in.