Aller au contenu principal

pullAt()

pullAt<T>(array, indexes): T[]

Removes elements from array at specified indexes and returns removed elements.

DEPRECATED

Use .filter() with index check for immutable operations.

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.

indexes: number[]

The indexes of elements to remove.


Returns: T[]

An array of removed elements.


Since

2.0.0


Also known as

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


Example

// ❌ Deprecated approach (mutates array)
const array = ['a', 'b', 'c', 'd'];
const pulled = pullAt(array, [1, 3]);
console.log(pulled); // ['b', 'd']
console.log(array); // ['a', 'c']

// ✅ Recommended approach (immutable)
const array = ['a', 'b', 'c', 'd'];
const indexesToRemove = new Set([1, 3]);
const remaining = array.filter((_, i) => !indexesToRemove.has(i));
const pulled = array.filter((_, i) => indexesToRemove.has(i));
console.log(pulled); // ['b', 'd']
console.log(remaining); // ['a', 'c']
console.log(array); // ['a', 'b', 'c', 'd'] (unchanged)

How it works?

Removes elements at specified indexes and returns them (mutates).

Processing Flow

Common Inputs

ArrayIndexesPulledRemaining
['a','b','c','d'][1, 3]['b','d']['a','c']
[1, 2, 3][0][1][2, 3]

Deprecated: Use .filter() with index check for immutable operations.


Use Cases

Extract elements at indices 📌

Remove and return elements at specific indices.

const items = ["a", "b", "c", "d", "e"];
const indices = new Set([1, 3]);
const extracted = items.filter((_, i) => indices.has(i));
const remaining = items.filter((_, i) => !indices.has(i));
// extracted: ["b", "d"], remaining: ["a", "c", "e"]

Remove by position

Remove elements at known positions.

const arr = [10, 20, 30, 40, 50];
const keep = arr.filter((_, i) => i !== 2);
// => [10, 20, 40, 50]

Extract multiple positions

Get elements at multiple indices.

const data = ["a", "b", "c", "d"];
[0, 2].map(i => data[i]);
// => ["a", "c"]