remove items from array that are in another array code example

Example 1: remove item from array if exists in another array

$filteredFoo = array_diff($foo, $bar);

Example 2: remove an element in array useing another array

Remove an item comparing from other array

const toRemoveMap = toRemove.reduce(
  function(memo, item) {
    memo[item] = memo[item] || true;
    return memo;
  },
  {} // initialize an empty object
);

const filteredArray = myArray.filter(function (x) {
  return toRemoveMap[x];
});

// or, if you want to use ES6-style arrow syntax:
const toRemoveMap = toRemove.reduce((memo, item) => ({
  ...memo,
  [item]: true
}), {});

const filteredArray = myArray.filter(x => toRemoveMap[x.id]);

From <https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array>