W
W
wufapexef2018-03-19 23:44:14
JavaScript
wufapexef, 2018-03-19 23:44:14

How to add only unique elements to an array?

What is an efficient way (there are probably many) to add only unique elements to an array, avoiding duplicates? Interested in the native way and, if there is lodash. Both methods are useful. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-03-19
@Stalker_RED

Set objects allow you to store unique values ​​of any type, both primitives and other types of objects.
Syntax
https://developer.mozilla.org/en/docs/Web/JavaScript...

S
SagePtr, 2018-03-19
@SagePtr

if (a.indexOf(x) === -1) a.push(x);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question