Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: How to remove duplicates from Array [duplicate]

I have an array:

[     1029,     1008,     1040,     1019,     1030,     1009,     1041,     1020,     1031,     1010,     1042,     1021,     1030,     1008,     1045,     1019,     1032,     1009,     1049,     1022,     1031,     1010,     1042,     1021, ] 

Now I want to remove all the duplicates from it. Is there any method in NodeJs which can directly do this.

like image 735
Yo Yo Saty Singh Avatar asked Apr 23 '14 07:04

Yo Yo Saty Singh


People also ask

How do you remove duplicates from an array of arrays?

To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.

How do you remove duplicates from array of objects?

To remove the duplicates from an array of objects:Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.

How do you remove duplicate array elements in JavaScript?

Use the filter() method: The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.

How do you prevent duplicate arrays?

To prevent adding duplicates to an array:Use the indexOf() method to check that the value is not present in the array. The indexOf method returns -1 if the value is not contained in the array. If the condition is met, push the value to the array.


1 Answers

No, there is no built in method in node.js, however there are plenty of ways to do this in javascript. All you have to do is look around, as this has already been answered.

uniqueArray = myArray.filter(function(elem, pos) {     return myArray.indexOf(elem) == pos; }) 
like image 100
mihai Avatar answered Sep 29 '22 07:09

mihai