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


First of all:

  1. Why would you define functions within your function and call them each only once? I would just skip all those arrow functions.
  2. Use const in place of let unless you absolutely need to reassign these variables, which you don't in this case.

Your solution seems to work, only 2 things I can think of to improve (besides not defining functions then calling them once as mentioned above):

  1. You are calling extractValue() on the SAME set of parameters for every member of arrayTotal, that's redundant.
  2. You can replace that .every() call with .includes().

A revised version would look like this:

const substractObjectsFromArray = (arrayTotal, arrayToSubstract, propertyOfReference) => {
  const idsToSubtract = arrayToSubtract.map(element => element[propertyOfReference]);
  return arrayTotal.filter(element => idsToSubtract.includes(element[propertyOfReference]);

Thanks you are right, it's a good way to improve it.

code of conduct - report abuse