Answers for "remove item from array if exists in another array"

1

remove item from array if exists in another array

myArray = myArray.filter( ( el ) => !toRemove.includes( el ) );
Posted by: Guest on September-30-2021
1

remove item from array if exists in another array

myArray = myArray.filter( function( el ) {
  return !toRemove.includes( el );
} );
Posted by: Guest on September-30-2021
0

remove item from array if exists in another array

$filteredFoo = array_diff($foo, $bar);
Posted by: Guest on December-07-2020
1

remove item from array if exists in another array

myArray = myArray.filter( function( el ) {
  return toRemove.indexOf( el ) < 0;
} );
Posted by: Guest on September-30-2021
0

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>
Posted by: Guest on May-05-2021

Code answers related to "remove item from array if exists in another array"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language