DEV Community

Md. Khalid Hossen
Md. Khalid Hossen

Posted on

2

Remove multiple item from an array using array in JavaScript.

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay