Javascript:从2个数组中查找不匹配的对象

时间:2019-04-27 08:40:56

标签: javascript arrays object

我有2个对象数组

var arr1 = [{id: "145", firstname: "dave", lastname: "jones"},
            {id: "135", firstname: "mike",lastname: "williams"},
            {id: "148", firstname: "bob",lastname: "michaels"}];
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"},
            {id: "135", firstname: "mike", lastname: "williams"},
            {id: "148", firstname: "bob", lastname: "michaels"}];

我想找到仅在其中一个数组中存在id的对象,然后将该对象记录到控制台或将对象推送到新数组。

因此我想结束

var arr1 = [{id: "145", firstname: "dave", lastname: "jones"}]
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"}]

我尝试使用forEach循环并将匹配的ID拼接到数组之外

arr1.forEach(function(element1, index1) {
                let arr1Id = element1.id;
                arr2.forEach(function(element2, index2) {
                    if (arr1Id === element2.id) {
                        arr1.splice(element1, index1)
                        arr2.splice(element2, index2)

                };
            });
        });


console.log(arr1);
console.log(arr2);

但是我最终还是

arr1

[ { id: '135', firstname: 'mike', lastname: 'williams' },
  { id: '148', firstname: 'bob', lastname: 'michaels' } ]

arr2

 [ { id: '135', firstname: 'mike', lastname: 'williams' },
  { id: '148', firstname: 'bob', lastname: 'michaels' } ]

3 个答案:

答案 0 :(得分:7)

您可以为每个数组的id取一个Set并通过检查是否存在来过滤另一个数组。

var array1 = [{ id: "145", firstname: "dave", lastname: "jones" }, { id: "135", firstname: "mike", lastname: "williams" }, { id: "148", firstname: "bob", lastname: "michaels" }],
   array2 = [{ id: "146", firstname: "dave", lastname: "jones" }, { id: "135", firstname: "mike", lastname: "williams" }, { id: "148", firstname: "bob", lastname: "michaels" }],
   set1 = new Set(array1.map(({ id }) => id)),
   set2 = new Set(array2.map(({ id }) => id)),
   result1 = array1.filter(({ id }) => !set2.has(id)),
   result2 = array2.filter(({ id }) => !set1.has(id));

console.log(result1);
console.log(result2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:3)

只需在!arr.some()内使用Array.prototype.filter()

const arr1 = [{id: "145", firstname: "dave", lastname: "jones"},{id: "135", firstname: "mike",lastname: "williams"},{id: "148", firstname: "bob",lastname: "michaels"}],
 arr2 = [{id: "146", firstname: "dave", lastname: "jones"},{id: "135", firstname: "mike", lastname: "williams"},{id: "148", firstname: "bob", lastname: "michaels"}],
 newArr1 = arr1.filter(x => !arr2.some(y => y.id === x.id)),
 newArr2 = arr2.filter(x => !arr1.some(y => y.id === x.id));

console.log(newArr1, newArr2);

答案 2 :(得分:0)

您好,请尝试使用过滤器和findindex的组合,如下面的代码片段,并让我知道。

var arr1 = [{id: "145", firstname: "dave", lastname: "jones"},
            {id: "135", firstname: "mike",lastname: "williams"},
            {id: "148", firstname: "bob",lastname: "michaels"}];
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"},
            {id: "135", firstname: "mike", lastname: "williams"},
            {id: "148", firstname: "bob", lastname: "michaels"}];
            
let unmatchedArr1 = arr1.filter(element => {
 let targetIndex = arr2.findIndex(e => element.id === e.id);
 return targetIndex >= 0 ? false : true;
})
let unmatchedArr2 = arr2.filter(element => {
 let targetIndex = arr1.findIndex(e => element.id === e.id);
 return targetIndex >= 0 ? false : true;
})

console.log(unmatchedArr1);
console.log(unmatchedArr2);