最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Finding unmatched objects from 2 arrays - Stack Overflow

programmeradmin3浏览0评论

I have 2 arrays of objects

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"}];

I want to find the objects where the id exists in only one of the arrays and either log the object to the console or push the object to a new array.

Therefore I want to end up with

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

I tried using a forEach loop and splicing matching id's out of the array

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);

But I ended up with

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' } ]

I have 2 arrays of objects

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"}];

I want to find the objects where the id exists in only one of the arrays and either log the object to the console or push the object to a new array.

Therefore I want to end up with

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

I tried using a forEach loop and splicing matching id's out of the array

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);

But I ended up with

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' } ]
Share Improve this question asked Apr 27, 2019 at 8:40 DrumBongoDrumBongo 1231 gold badge1 silver badge9 bronze badges 3
  • use Array.filter, it's designed to do this kind of job. – apple apple Commented Apr 27, 2019 at 8:44
  • Would I pass in a function that matches 2 id's? – DrumBongo Commented Apr 27, 2019 at 8:50
  • there are now answers show how to do it. but answer your question, no, you should pass a function accpting object of type { id: string, firstname: string, lastname: string } and return true (keep) or false (exclude) – apple apple Commented Apr 27, 2019 at 9:01
Add a ment  | 

3 Answers 3

Reset to default 6

You could take a Set for every array's id and filter the other array by checking the existence.

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; }

Just use !arr.some() inside a 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);

Hello please try using bination of filter and findindex like the below snippet and let me know.

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);

发布评论

评论列表(0)

  1. 暂无评论