I am looking for a simple solution to compare just the "id" values and "type" in from two different arrays of objects as the following:
var arr_a = [{id:1, name:"thumbler", type:"add"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"}];
var arr_b = [{id:1, name:"thumbler", type:"remove"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"},
{id:4, name:"fork", type:"add"}];
and i wanna a result like this:
var arr_c = [{id:1, name:"thumbler", type:"remove"},
{id:4, name:"fork", type:"add"}]
I tried to use filter and some (from here) but doesn’t work:
var arr_c = arr_b.filter(x=> arr_a.filter(y=> y.id === x.id && y.action_type === x.action_type).length >0);
Any suggestions?
2
Answers
It can be done, and, it needs a couple of steps as you are just not checking the duplicates.
Working code below
You can use
every
andsome
methods of the array to achieve your result. See the snippet below.Full working code:-