I'm new in JavaScript programming and I have two object arrays that have the following structure:
myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}];
mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}];
I need to get two separate arrays containing the values of key foo
, the first containing the ones that are in the first array but not in the second, based on the value of key foo
, and the second that are in mySecondObjArray
but not in myFirstObjArray
.
Is there a way to do this without
for(i=0;i<myFirstObjArray.length;i++)
for(j=0;j<mySecondObjArray .length;j++)
{...build first array here}
for(i=0;i<mySecondObjArray .length;i++)
for(j=0;j<myFirstObjArray.length;j++)
{...build second array here}
? Perhaps my question is a duplicate one that I didn't find, so please be gentle.
Expected output:
firstArray = [{foo: 1}, {foo: 3}];
secondArray = [{foo: 2}, {foo: 5}];