3 ms·
Personally I like this one, although it only works for an array of primitive types. const unique = [...new Set(arr)];
by superfrank 7y ago
Personally I like this one, although it only works for an array of primitive types.
const unique = [...new Set(arr)];
- _bxg1 7y agoThe other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.
- superfrank 7y agoOh, for sure. I know .filter() has it's place, so if you need anything beyond making the array unique, I'd definitely use your solution. The one I posted just blew my mind a little the first time I saw it, so I love to share it. I guess I never really considered spreading a set.