javascript
Remove duplicate values from an array in Javascript
If you want to remove all duplicate values from a javascript array or want to get the unique values array then you can use the below code.
var my_array = [5, 4, 7, 8, 9, 2, 7, 5, 4, 8, 7];
// First Method
unique_array = my_array.filter((item, index, array) => array.indexOf(item) === index);
console.log("Unique Array is : " + unique_array);
// Second Method
var another_uni_arr = [...new Set(my_array)];
console.log("Another Unique Array is : " + another_uni_arr);
In the first method, we are using Array.filter() method and checking if the item already exists in the array or not by using .indexOf() method.
Example Demo
Was this helpful?
Similar Posts
- Check if array contains duplicate values in Javascript
- How to Remove duplicate elements from array in JavaScript ?
- Remove duplicate items from array in Javascript
- Join two arrays and remove duplicate elements using Javascript
- Remove empty, false, undefined values from array Javscript
- Remove all false, null, undefined and empty values from array using Lodash
- Remove duplicates from an array and return unique values in O(n) complexity