Did you know that you can navigate the posts by swiping left and right?
Here is a collection of snippets for manipulating arrays using es6 features in an immutable way aiming towards a more functional style of programming. In a subsequent post, I’m going to write about similar patterns but with objects.
Further, these patterns help if for whatever reason you cannot use immutable.js in your project.
What if you want to get the first element of the array? What do you do? I used to do:
const first = anArray[0];
Using array destructuring, in es6, you can now do instead:
const [first] = anArray;
The Array::shift method can be used to remove an element from the beginning of the array. If you want to keep the original array unchanged while performing an operation equivalent to shift you can do:
const fruits = ['apple', 'orange', 'pear'];
const [apple, ...restOfFruits] = fruits;
//restOfFruits contains ['orange', 'pear']
Add to the end of the array. Just like Array::push
const fruits = ['apple', 'pear']
const moreFruits = [...fruits, 'orange']
//moreFruits now contains ['apple', 'pear', 'orange']
You can generalize the pattern this way.
const fruits = ['apple', 'pear'];
const moreFruits = ['grape', ...fruits, 'orange'];
//moreFruits now contains ['grape','apple', 'pear', 'orange']
In other words, they always return/represent a value. This means you can cleanly use them for writing a more functional style javascript. For instance in a reduce operation. Like so:
const fruits = ['apple', 'pear'];
const reverseFruits = fruits.reduce((acc, fruit) => [fruit, ...acc], []);
// reverseFruits contains ['pear', 'apple']
You can read more about expressions and statements here.
courtesy of runkit:
To be continued…