Categories
JavaScript Answers

How to use JavaScript lodash push to an array only if value doesn’t exist?

Spread the love

To use JavaScript lodash push to an array only if value doesn’t exist, we use sets.

For instance, we write

const s = new Set();

s.add("hello");
s.add("world");
s.add("hello");

to create a set with the Set constructor.

And then we call add to add 'hello' and 'world'.

'hello' won’t be added a 2nd time since it already exists.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *