Remove multiple item from an array using array in JavaScript.

Md. Khalid Hossen - Oct 26 '22 - - Dev Community

Suppose your task is removed multiple items from an array using an array:

My Working procedure:

  1. At first remove Items Array make it object
  2. Then filter array with conditionally where check is it available into that object or not.
  3. If not available return items

*Example: *

  // array which holds all values
  const array = [2, 4, 6, 3];

  // array of values that needs to be deleted
  const itemsToDeleteArr = [4, 6];

  // make an object to hold values from itemsToDeleteArr
  const itemsToDeleteSet = new Set(itemsToDeleteArr);

  // use filter() method
  // to filter only those elements
  // that need not to be deleted from the array
  const newArr = namesArr.filter((item) => {
    // return those elements not in the namesToDeleteSet
    return !itemsToDeleteSet.has(item);
  });

  console.log(newArr); // [2, 3]
Enter fullscreen mode Exit fullscreen mode
. . . . .