javascript collections with unique values code example
Example 1: array unique values javascript
const myArray = ['a', 1, 'a', 2, '1'];
const unique = [...new Set(myArray)];
Example 2: 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);
Example 3: array with unique values javascript
let uniqueItems = [...new Set(items)]