javascript get unique objects in array code example
Example 1: 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 2: javascript find unique values in array of objects
var flags = [], output = [], l = array.length, i;
for( i=0; i<l; i++) {
if( flags[array[i].age]) continue;
flags[array[i].age] = true;
output.push(array[i].age);
}