Sunday, 16 February 2020

Remove duplicate by using one line of code – JavaScript

JavaScriptES6Set

There are many different ways to remove duplicates. Here we will be using JavaScript ES6 feature, see below example.

Sample list


let list = [1,2,2,3,4,5];

Now we need to remove duplicates.


new Set(list);

We have used JavaScript Set operator.

See the result

But we need in array format, for that we can use JavaScript spread operator.


[...new Set(list)]

See the result now

Here we have discussed about removing duplicates using JavaScript ES6 feature.

Related Info

1. How to prevent modifying an Object – JavaScript.

2. How to solve JavaScript heap out of memory issue in Angular project.

3. How to rename key of an Object.

No comments :

Post a comment