javascript - Remove all elements contained in another array - Stack Overflow

PHOTO EMBED

Thu Jun 15 2023 06:35:30 GMT+0000 (Coordinated Universal Time)

Saved by @Harsh_Sh #javascript

//Using the new ES6 Syntax

    console.log(["a", "b", "c", "d", "e", "f", "g"].filter(el => !["b", "c", "g"].includes(el)));

    // OR

    // Main array
    let myArray = ["a", "b", "c", "d", "e", "f", "g"];

    // Array to remove
    const toRemove = ["b", "c", "g"];

    const diff = () => (myArray = myArray.filter((el) => !toRemove.includes(el)));
  console.log(diff()); // [ 'a', 'd', 'e', 'f' ]

    // OR

    const diff2 = () => {
      return myArray = myArray.filter((el) => !toRemove.includes(el));
    };
    console.log(diff2()); // [ 'a', 'd', 'e', 'f' ]
content_copyCOPY

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