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