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:
and more, see in MDN documentation.
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?