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.
note
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โ
| Array | Indexes | Pulled | Remaining |
|---|---|---|---|
['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"]
Deprecated: Use