Filter out matching array elements

OSCC · JavaScript, Array · Oct 22, 2020

Filters out the elements of an array that have one of the specified values.

  • Use Array.prototype.includes() to find values to exclude.
  • Use Array.prototype.filter() to create an array excluding them.
const without = (arr, ...args) => arr.filter(v => !args.includes(v));
without([2, 1, 2, 3], 1, 2); // [3]

Would you like to help us improve 30 seconds of code?Take a quick survey

Recommended snippets