Aller au contenu principal

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"]