JavaScript: 4 Great ES6 Features You Should Start Using

Remove duplicates from array, create key => value pairs and search in array with new available functions.

Remove Duplicates From Array

Set is an object, which stores only unique values. It is easy to convert array into Set and back, but maybe you don’t even need it.

With Set you can do many common operations like:

  1. get length of Set
  2. iterate throught all items
  3. check if Set contains value

and more, see in MDN documentation.

Create key => value Pairs in JavaScript

Unlike in other programming languages, e.g. PHP, it isn’t possible to create key => value pairs in simple JavaScript array.

In ES6 (or ECMAScript 2015, if you wish) implementation, new Map object is added, which does exactly this job:

What if you need to apply some more specific operation, you can do only with array?

No problem, Map object can be converted into array in the same way as Set. The result is 2-dimensional array. And, of course, conversion works vice versa, too.

Search if Array Contains Wanted Item

The problem: you need to check if array contains specific value.

In the past, indexOf function was often used to find a position of the first occurance, and if not presented, -1 is returned.

This works, but in many case you only need to return boolean value true or false. This is now possible with includes function:

Test Array Values and Find First Which Satisfies Your Condition

The problem: you need to find a first value in array, which fits condition.

Condition is meant to be a function. Couple of years ago, the iteration throught all items would make a job.

Thanks to the new “find” function, it can be even simpler:

Finally, what is important to mention, “find” function is also usable with arrays containing objects.

Do you use already these ES6 features?