30秒学会 JavaScript 片段 · 2023年11月25日

30秒学会 JavaScript 片段 – Filter out matching array elements

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]

翻译自:https://www.30secondsofcode.org/js/s/filter-out-matching-array-elements