how to compare two object of array and output mismatching values only?

Advertisements

I am trying to find the most efficient way to output an object of array having mismatching values only. Here are two object of arrays:

var arrA = [
   {Name: "A"},
   {Name: "C"},
   {Name: "F"},
   {Name: "G"},
]

var arrB = [
   {Name: "C"},
   {Name: "J"},
   {Name: "I"},
]

Here is the result I am expecting:

[
   {Name: "A"},
   {Name: "F"},
   {Name: "G"},
]

Please advise. Thank you.

>Solution :

Definitely not the most efficient way in terms of performance but not a bad approach if lines of code is what you pay attention to.

var arrA = [{Name: "A"}, {Name: "C"}, {Name: "F"}, {Name: "G"}];
var arrB = [{Name: "C"}, {Name: "J"}, {Name: "I"}];

var result = arrA.filter(a => !arrB.some(b => b.Name === a.Name));

console.log(result);

Leave a ReplyCancel reply