0
# Lodash pullAt
1
2
The `pullAt` function removes elements from an array corresponding to the given indexes and returns an array of the removed elements. Unlike `_.at`, this method mutates the original array. Indexes may be specified as individual arguments or as arrays of indexes.
3
4
## Package Information
5
6
- **Package Name**: lodash
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash`
10
- **Function Location**: lodash v3.2.0
11
12
## Core Imports
13
14
```javascript
15
import _ from "lodash";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const _ = require("lodash");
22
```
23
24
Note: Lodash v3.2.0 does not support ES6 named imports. The pullAt function is accessed as a method of the main lodash object.
25
26
## Basic Usage
27
28
```javascript
29
const _ = require("lodash");
30
31
// Basic usage with array of indexes
32
const array = [5, 10, 15, 20];
33
const removed = _.pullAt(array, [1, 3]);
34
35
console.log(array); // => [5, 15]
36
console.log(removed); // => [10, 20]
37
```
38
39
## Architecture
40
41
The `pullAt` function is part of Lodash's Array category methods, designed for precise array manipulation. It operates through:
42
43
- **Index Processing**: Uses `baseFlatten` to normalize individual and array index arguments into a single array
44
- **Element Extraction**: Leverages `baseAt` to capture elements at specified positions before removal
45
- **Safe Removal**: Sorts indexes in ascending order and removes elements from highest to lowest index to prevent index shifting issues
46
- **Mutation Pattern**: Directly modifies the input array while returning the removed elements, following Lodash's mutation conventions for functions ending in "At"
47
48
This function complements Lodash's broader array manipulation ecosystem, working alongside methods like `pull`, `remove`, and `at`.
49
50
## Capabilities
51
52
### pullAt Function
53
54
Removes elements from an array at specified indexes and returns the removed elements.
55
56
```javascript { .api }
57
/**
58
* Removes elements from array corresponding to the given indexes and returns
59
* an array of the removed elements. Indexes may be specified as an array of
60
* indexes or as individual arguments.
61
*
62
* Note: Unlike _.at, this method mutates array.
63
*
64
* @param {Array} array - The array to modify
65
* @param {...(number|number[])} [indexes] - The indexes of elements to remove,
66
* specified as individual indexes or arrays of indexes
67
* @returns {Array} Returns the new array of removed elements
68
*/
69
_.pullAt(array, ...indexes);
70
```
71
72
**Parameter Details:**
73
74
- `array` (Array): The array to modify. If falsey, treated as empty array
75
- `indexes` (...(number|number[])): Variable arguments representing indexes to remove. Can be:
76
- Individual index arguments: `pullAt(array, 1, 3, 0)`
77
- Arrays of indexes: `pullAt(array, [1, 3])`
78
- Mixed format: `pullAt(array, [3], [0, 2])`
79
80
**Return Value:**
81
82
Returns an array containing the removed elements in the order they were specified in the indexes parameter. If an index doesn't exist, `undefined` is included in that position.
83
84
**Behavior Notes:**
85
86
- **Mutates the original array** - elements are permanently removed
87
- **Index handling**:
88
- Handles unsorted indexes correctly (sorts internally before removal)
89
- Handles repeated indexes (returns multiple copies of the same element)
90
- Uses `undefined` for nonexistent indexes
91
- Ignores non-integer indexes (like -1, 1.1, or string keys)
92
- **Empty arguments**: Returns empty array when no indexes provided
93
- **Falsey arrays**: Works with falsey array arguments when indexes are provided
94
95
**Usage Examples:**
96
97
```javascript
98
// Multiple individual indexes
99
const data = ['a', 'b', 'c', 'd'];
100
const removed = _.pullAt(data, 3, 0, 2);
101
console.log(data); // => ['b']
102
console.log(removed); // => ['d', 'a', 'c']
103
104
// Unsorted indexes
105
const items = [1, 2, 3, 4];
106
const pulled = _.pullAt(items, [1, 3, 0]);
107
console.log(items); // => [3]
108
console.log(pulled); // => [2, 4, 1]
109
110
// Repeated indexes
111
const values = [1, 2, 3, 4];
112
const duplicates = _.pullAt(values, [0, 2, 0, 1, 0, 2]);
113
console.log(values); // => [4]
114
console.log(duplicates); // => [1, 3, 1, 2, 1, 3]
115
116
// Nonexistent indexes
117
const letters = ['a', 'b', 'c'];
118
const result = _.pullAt(letters, [2, 4, 0]);
119
console.log(letters); // => ['b']
120
console.log(result); // => ['c', undefined, 'a']
121
122
// Mixed argument formats
123
const mixed = ['a', 'b', 'c', 'd'];
124
const output = _.pullAt(mixed, [3], [0, 2]);
125
console.log(mixed); // => ['b']
126
console.log(output); // => ['d', 'a', 'c']
127
128
// No indexes provided
129
const unchanged = ['x', 'y', 'z'];
130
const empty = _.pullAt(unchanged);
131
console.log(unchanged); // => ['x', 'y', 'z']
132
console.log(empty); // => []
133
```