DEV Community

Discussion on: We have 2 arrays of objects of the same class. How to substract the 1st from the 2nd, based only on one property?

Collapse
 
dhilipkmr profile image
Dhilip kumar

I'm no expert just tried.

Not tested.

Store all the various props of arrayToSubtract in an Object.

var referenceObj = {}
arrayToSubstract.forEach((item) => referenceObj[item[propertyOfReference]] = true;);

//Reference object keys should not be in arrayTotal

var outputArr = arrayTotal.filter((item) => !referenceObj[item[propertyOfReference]);

Collapse
 
axelledrouge profile image
AxelleDRouge

Interesting way to resolve the problem, it should work :)