Array utility function that removes elements at specified indexes and returns the removed elements
npx @tessl/cli install tessl/npm-lodash--pullat@4.6.00
# Lodash pullAt
1
2
Lodash pullAt is an array utility function that removes elements from an array at specified indexes and returns the removed elements. Unlike `_.at`, this method mutates the original array. It handles indexes flexibly, accepting them individually or as arrays.
3
4
## Package Information
5
6
- **Package Name**: lodash
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash`
10
11
## Core Imports
12
13
```javascript
14
import _ from "lodash";
15
```
16
17
For modular usage:
18
19
```javascript
20
import pullAt from "lodash.pullat";
21
```
22
23
CommonJS:
24
25
```javascript
26
const _ = require("lodash");
27
// or
28
const pullAt = require("lodash.pullat");
29
```
30
31
## Basic Usage
32
33
```javascript
34
import _ from "lodash";
35
36
// Remove elements at specific indexes
37
const array = [5, 10, 15, 20];
38
const removed = _.pullAt(array, 1, 3);
39
40
console.log(array); // => [5, 15] (original array is mutated)
41
console.log(removed); // => [10, 20] (removed elements are returned)
42
43
// Using array of indexes
44
const data = ['a', 'b', 'c', 'd', 'e'];
45
const extracted = _.pullAt(data, [0, 2, 4]);
46
47
console.log(data); // => ['b', 'd'] (mutated)
48
console.log(extracted); // => ['a', 'c', 'e'] (extracted elements)
49
```
50
51
## Capabilities
52
53
### Array Element Removal
54
55
Removes elements from array corresponding to indexes and returns an array of removed elements.
56
57
```javascript { .api }
58
/**
59
* Removes elements from `array` corresponding to `indexes` and returns an
60
* array of removed elements.
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 individually or in arrays.
67
* @returns {Array} Returns the new array of removed elements.
68
*/
69
function pullAt(array, ...indexes);
70
```
71
72
**Parameters:**
73
- `array` (Array): The array to modify
74
- `...indexes` (...(number|number[])): The indexes of elements to remove, specified individually or in arrays
75
76
**Returns:**
77
- `Array`: Returns the new array of removed elements
78
79
**Behavior:**
80
- **Mutating**: The original array is modified in place
81
- **Flexible Index Input**: Accepts indexes as individual arguments or nested arrays
82
- **Index Processing**: Indexes are flattened and sorted before processing to ensure correct removal
83
- **Return Value**: Returns an array containing the elements that were removed
84
85
**Usage Examples:**
86
87
```javascript
88
// Individual indexes
89
const arr1 = [1, 2, 3, 4, 5];
90
const removed1 = _.pullAt(arr1, 0, 2, 4);
91
// arr1: [2, 4]
92
// removed1: [1, 3, 5]
93
94
// Array of indexes
95
const arr2 = ['x', 'y', 'z', 'a', 'b'];
96
const removed2 = _.pullAt(arr2, [1, 3]);
97
// arr2: ['x', 'z', 'b']
98
// removed2: ['y', 'a']
99
100
// Mixed individual and array indexes
101
const arr3 = [10, 20, 30, 40, 50, 60];
102
const removed3 = _.pullAt(arr3, 0, [2, 4], 5);
103
// arr3: [20, 40]
104
// removed3: [10, 30, 50, 60]
105
106
// Empty array handling
107
const arr4 = [];
108
const removed4 = _.pullAt(arr4, 0, 1);
109
// arr4: []
110
// removed4: []
111
112
// Out of bounds indexes (safely ignored)
113
const arr5 = [1, 2, 3];
114
const removed5 = _.pullAt(arr5, 1, 10, 20);
115
// arr5: [1, 3]
116
// removed5: [2]
117
```
118
119
## Access Patterns
120
121
### Full Library Access
122
123
When using the complete lodash library:
124
125
```javascript
126
import _ from "lodash";
127
const result = _.pullAt(array, ...indexes);
128
```
129
130
### Modular Access
131
132
When using the standalone pullAt package:
133
134
```javascript
135
// Install: npm install lodash.pullat
136
import pullAt from "lodash.pullat";
137
const result = pullAt(array, ...indexes);
138
139
// CommonJS
140
const pullAt = require("lodash.pullat");
141
const result = pullAt(array, ...indexes);
142
```
143
144
### Browser CDN Access
145
146
```html
147
<script src="https://cdn.jsdelivr.net/npm/lodash@4.6.0/lodash.min.js"></script>
148
<script>
149
const result = _.pullAt(array, ...indexes);
150
</script>
151
```
152
153
## Related Functions
154
155
These lodash functions provide similar array manipulation capabilities:
156
157
- `_.pull(array, ...values)` - Removes specified values from array
158
- `_.pullAll(array, values)` - Removes array of values from array
159
- `_.pullAllBy(array, values, iteratee)` - Removes values using iteratee
160
- `_.pullAllWith(array, values, comparator)` - Removes values using comparator
161
- `_.remove(array, predicate)` - Removes elements matching predicate
162
- `_.at(object, ...paths)` - Gets values at specified paths (non-mutating)
163
164
## Implementation Notes
165
166
- Uses internal `rest` function to create variadic parameter handling
167
- Indexes are flattened using `baseFlatten` and converted to strings
168
- Uses `baseAt` to extract elements before removal
169
- Uses `basePullAt` for the actual array mutation with sorted indexes
170
- Indexes are sorted in ascending order before processing to maintain correct removal sequence