EDITED !!
我有一个对象array
:
Array 0:[{id:"a1" , code:"123" , name:"a"},
{id: "a2", code: "222" , name: "a"},
{id: "b1", code: "433", name: "b"}]
Array 1:[{id:"a1" , code:"123" , name:"a"},
{id: "b2", code: "211" , name: "b"},
{id: "b1", code: "433", name: "b"}]
我想获取“name”没有重复值的对象数组。并将其存储到另一个array
:
Result:
Array 0:{id: "b1", code: "433", name:"b"}
Array 1: {id:"a1" , code:"123" , name:"a"}
如何获取name
没有重复值的对象数组?
我在一些线程上发现的只是从数组中删除重复项而不是没有重复的数组。提前谢谢!
答案 0 :(得分:1)
function filterByName(array) {
var counts = {};
array.forEach(function(obj) {
counts[obj.name] ? ++counts[obj.name] : (counts[obj.name] = 1);
});
return array.filter(function(obj) {
return counts[obj.name] === 1;
});
}
var filteredArray1 = filterByName(array1);
var filteredArray2 = filterByName(array2);
功能说明