Object1 = {connectorIndex: 1, nodeID: 6, Connectors: Object}
Object2 = {connectorIndex: 1, nodeID: 6, Connectors: Object}
Connector: {name: "ALAND", key: "", description: "Departure country (country from which the goods are sent)"}
同一阵列中有两个对象。连接器对象是相同的。如何删除重复的元素并获得带有一个对象的最终数组?
var array = [object 1, object 2];
object 2
是要从数组中删除的重复项。 最佳答案
如果您正在寻找完全匹配的内容,则可以这样做:
function remove_duplicates(objectsArray) {
var usedObjects = {};
for (var i=objectsArray.length - 1;i>=0;i--) {
var so = JSON.stringify(objectsArray[i]);
if (usedObjects[so]) {
objectsArray.splice(i, 1);
} else {
usedObjects[so] = true;
}
}
return objectsArray;
}
var objectsArray = [{a:'foo',b:'bar'}, {a:'foo',b:'bar'}];
var clean = remove_duplicates(objectsArray);