Aller au contenu principal

pull()

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

Removes all given values from array using SameValueZero for equality comparisons.

DEPRECATED

Use difference instead.

Reason:
Pithos design philosophy always favors immutability.

remarque

Mutates the array in place.


Type Parameters

T: T

The type of elements in the array.


Parameters

array: T[]

The array to modify.

values: ...T[]

The values to remove.


Returns: T[]

The mutated array.


Since

2.0.0


Also known as

pull (Lodash, es-toolkit) · remove (Antfu) · without (Remeda) · ❌ (Radashi, Ramda, Effect, Modern Dash)


Example

const array = [1, 2, 3, 1, 2, 3];
pull(array, 2, 3);
console.log(array); // [1, 1]

How it works?

Removes all given values from an array (mutates). Deprecated: Use immutable filter() or without() instead.

Native Equivalent

// ❌ pull(arr, ...values)  // mutates
// ✅ arr.filter(x => !values.includes(x)) // immutable

Use Cases

Remove values in place 📌

Remove specific values from an array (mutating).

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

Remove single value

Filter out a specific value.

const tags = ["js", "ts", "css", "js"];
tags.filter(t => t !== "js");
// => ["ts", "css"]

Remove multiple values

Exclude multiple values using a Set.

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