make an array unique in javascript code example
Example 1: js unique using set
let uniqueArray = [...new Set([5,5,2,2,2,4,2])];
Example 2: javascript find unique values in array
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
Example 3: how to find unique elements in array in javascript
let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);
Example 4: how to make array uniq
let uniqueArray = [...new Set([5,5,2,2,2,4,2])];
Example 5: javascript remove duplicates from array
function toUniqueArray(a){
var newArr = [];
for (var i = 0; i < a.length; i++) {
if (newArr.indexOf(a[i]) === -1) {
newArr.push(a[i]);
}
}
return newArr;
}
var colors = ["red","red","green","green","green"];
var colorsUnique=toUniqueArray(colors);