Remove duplicate objects from a list
objects have arbitrary keys & values, uniqueness defined by combination of key & value
JavaScript
var data = [{'one':1},{'two':2},{'three':3},{'four':4},{'five':5},{'one':1}];
var tempMap = {}; // keep track of unique objects with key mapping to the object's key&value
var distinct = []; // resulting list containing only unique objects
var obj = null;
for (var i = 0; i < data.length; i++) {
obj = data[i];
for (var key in obj) { // look in the object eg. {'one':1}
if (obj.hasOwnProperty(key)) {
if (!tempMap.hasOwnProperty(key + obj[key])) { // not in map
tempMap[key + obj[key]] = obj; // then add it to map
distinct.push(obj); // add it to our list of distinct objects
}
break;
}
}
}
console.log(distinct);
console.log(data);