Given anarray:const myArr = [1, 2, 3, 4, 1, 1, 4], how to remove duplicates?
We can take advantage ofSet - it's a built-infunction constructor in JS.
const mySet = new Set(myArr)
This solves our immediate issue of removing the duplicates; now, to just turn this back into anarray.
const myNewArr = [...mySet]
We have takenmySet 👆🏽 and spread it out with.... Then, we have just wrapped up these 'loose elements' into a newarray as we see from the presence of[].
And, putting it all together, we can create a 'one-line utility function:'const removeDuplicates = (a) => [...new Set(a)]
You can see some other of the utility functions I routinely usehere.
Top comments(4)

- LocationBerlin
- EducationMSc in Electronics
- WorkFull Stack Developer
- Joined
Cool haven't thought of using "Set" yet. You could also filter directly on the array:
const unique = myArr.filter((value, index, array) => array.indexOf(value) === index);

That is significantly slower though, the Set uses an O(1) lookup so the routine is O(N) whereas indexOf would make it O(N**2)

- LocationBerlin
- EducationMSc in Electronics
- WorkFull Stack Developer
- Joined
Nice to know. Will try that next time.

It is working in this case, those objects are each unique. It isn't working by comparing the contents of the object for sure.
For further actions, you may consider blocking this person and/orreporting abuse





