Skip to main content

without()

without<T>(array, ...values): T[]

Creates an array excluding all given values.

Alias for difference.

DEPRECATED

Use difference from Arkhe instead.

Reason:
Alias of difference


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the array.


Parametersโ€‹

array: T[]โ€‹

The array to filter.

values: ...T[]โ€‹

The values to exclude.


Returns: T[]โ€‹

A new array without the specified values.


See Alsoโ€‹

difference


Sinceโ€‹

2.0.0


Also known asโ€‹

difference (Effect) ยท remove (Antfu) ยท without (Lodash, es-toolkit, Remeda, Ramda) ยท โŒ (Radashi, Modern Dash)


Exampleโ€‹

const numbers = [1, 2, 3, 4, 5];

// โŒ Deprecated approach
const filtered = without(numbers, 2, 4);
console.log(filtered); // [1, 3, 5]

// โœ… Recommended approach (Arkhe)
import { difference } from "pithos/arkhe/array/difference";
const filteredArkhe = difference(numbers, [2, 4]);
console.log(filteredArkhe); // [1, 3, 5]

How it works?โ€‹

Creates an array excluding specified values. Deprecated: Use array.filter() directly.

Native Equivalentโ€‹

// โŒ without(arr, 1, 2)
// โœ… arr.filter(x => x !== 1 && x !== 2)
// โœ… arr.filter(x => ![1, 2].includes(x))

Use Casesโ€‹

Remove specific values ๐Ÿ“Œโ€‹

Filter out specific values from an array.

const items = [1, 2, 3, 4, 5];
items.filter(x => x !== 3);
// => [1, 2, 4, 5]

Exclude multiple valuesโ€‹

Remove several values at once.

const all = [1, 2, 3, 4, 5];
const exclude = new Set([2, 4]);
all.filter(x => !exclude.has(x));
// => [1, 3, 5]

Remove from selectionโ€‹

Remove items from a multi-select.

const selected = ["a", "b", "c", "d"];
selected.filter(x => x !== "b");
// => ["a", "c", "d"]