Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vasfvitor/003256283418a5e573df7707958c128a to your computer and use it in GitHub Desktop.

Select an option

Save vasfvitor/003256283418a5e573df7707958c128a to your computer and use it in GitHub Desktop.
Remove all elements contained in another array
const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const toRemove = new Set(['b', 'c', 'g']);
--- method 1 ---
myArray = myArray.filter( ( el ) => !toRemove.includes( el ) );
--- method 2 ---
const difference = myArray.filter( x => !toRemove.has(x) );
console.log(difference); // ["a", "d", "e", "f"]
reference: https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment