Remove duplicate values from JS array [duplicate]

This question already has answers here: Get all unique values in a JavaScript array (remove duplicates) (83 answers) Closed 4 years ago. I have a very simple JavaScript array that may or may not contain duplicates. var names = [“Mike”,”Matt”,”Nancy”,”Adam”,”Jenny”,”Nancy”,”Carl”]; I need to remove the duplicates and put the unique values in a new array. … Read more

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values: function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = [‘a’, 1, ‘a’, 2, ‘1’]; var unique = a.filter(onlyUnique); console.log(unique); // [‘a’, 1, 2, ‘1’]  Run code snippetExpand … Read more