4

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' } ]
3
  • use Array.filter, it's designed to do this kind of job. Commented Apr 27, 2019 at 8:44
  • Would I pass in a function that matches 2 id's? 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) Commented Apr 27, 2019 at 9:01

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

This seems like very overhead'ish when a simple !arr.some() does the job.
in sense of big o, it has only a linear complexity.
I'm talking coding style. I wouldn't let that code pass in a review.
@NinaScholz does Set guarantee O(n)? I think it's O(nlog(n))
direct arr.some() may not be better, anyway.
|
3

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

Comments

0

Hello please try using combination 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.