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