withoutLoading saved progress…

without

_.without(array, ...values) returns a new array with every occurrence of the given values removed. It's the "subtract these items" operation — strip a few IDs out of a list, drop the selected tags, filter out sentinel values — without touching the original.

Implement without(arr, ...values). Return a copy of arr that excludes any element equal to one of values, compared with SameValueZero (so a NaN in the exclusion list removes NaN elements). Order is preserved and the input is never mutated.

Signature

function without(arr, ...values) {
  // returns a new array containing arr's elements that are NOT in `values`,
  // matched by SameValueZero.
}

Examples

without([1, 2, 3, 4], 2, 4);   // [1, 3]
without([1, 2, 1, 3, 1], 1);   // [2, 3] — every 1 removed
without([1, NaN, 2], NaN);     // [1, 2]  — NaN is matched and removed
without([1, 2, 3]);            // [1, 2, 3] — nothing to remove, full copy

Notes

  • SameValueZero — like ===, but NaN matches NaN. So NaN in values removes NaN elements.
  • Removes all occurrences — every element equal to an excluded value is dropped, not just the first.
  • Order preserved — the kept elements stay in their original order.
  • Objects by reference — only the same object reference is removed; two look-alike objects are different values.
  • New array — return a fresh array; never mutate arr.
Loading editor…