0
# Array Functions
1
2
Essential array manipulation utilities for transforming, filtering, and organizing data. These functions provide powerful ways to work with arrays while maintaining immutability principles.
3
4
## Capabilities
5
6
### Chunk
7
8
Creates an array of elements split into groups the length of size.
9
10
```javascript { .api }
11
/**
12
* Creates an array of elements split into groups the length of `size`.
13
* If `array` can't be split evenly, the final chunk will be the remaining elements.
14
*
15
* @param {Array} array The array to process.
16
* @param {number} [size=1] The length of each chunk.
17
* @returns {Array} Returns the new array containing chunks.
18
*/
19
function chunk(array, size);
20
```
21
22
**Usage Examples:**
23
24
```javascript
25
var chunk = require('lodash.chunk');
26
27
chunk(['a', 'b', 'c', 'd'], 2);
28
// => [['a', 'b'], ['c', 'd']]
29
30
chunk(['a', 'b', 'c', 'd'], 3);
31
// => [['a', 'b', 'c'], ['d']]
32
```
33
34
### Compact
35
36
Creates an array with all falsey values removed.
37
38
```javascript { .api }
39
/**
40
* Creates an array with all falsey values removed. The values
41
* `false`, `null`, `0`, `""`, `undefined`, and `NaN` are falsey.
42
*
43
* @param {Array} array The array to compact.
44
* @returns {Array} Returns the new array of filtered values.
45
*/
46
function compact(array);
47
```
48
49
**Usage Examples:**
50
51
```javascript
52
var compact = require('lodash.compact');
53
54
compact([0, 1, false, 2, '', 3]);
55
// => [1, 2, 3]
56
```
57
58
### Flatten
59
60
Flattens array a single level deep.
61
62
```javascript { .api }
63
/**
64
* Flattens `array` a single level deep.
65
*
66
* @param {Array} array The array to flatten.
67
* @returns {Array} Returns the new flattened array.
68
*/
69
function flatten(array);
70
```
71
72
**Usage Examples:**
73
74
```javascript
75
var flatten = require('lodash.flatten');
76
77
flatten([1, [2, [3, [4]], 5]]);
78
// => [1, 2, [3, [4]], 5]
79
```
80
81
### FlattenDeep
82
83
Recursively flattens array.
84
85
```javascript { .api }
86
/**
87
* Recursively flattens `array`.
88
*
89
* @param {Array} array The array to flatten.
90
* @returns {Array} Returns the new flattened array.
91
*/
92
function flattenDeep(array);
93
```
94
95
**Usage Examples:**
96
97
```javascript
98
var flattenDeep = require('lodash.flattendeep');
99
100
flattenDeep([1, [2, [3, [4]], 5]]);
101
// => [1, 2, 3, 4, 5]
102
```
103
104
### Uniq
105
106
Creates a duplicate-free version of an array.
107
108
```javascript { .api }
109
/**
110
* Creates a duplicate-free version of an array, using
111
* [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
112
* for equality comparisons, in which only the first occurrence of each element
113
* is kept.
114
*
115
* @param {Array} array The array to inspect.
116
* @param {boolean} [isSorted] Specify the array is sorted.
117
* @param {Function} [iteratee] The iteratee invoked per element.
118
* @param {*} [thisArg] The `this` binding of `iteratee`.
119
* @returns {Array} Returns the new duplicate free array.
120
*/
121
function uniq(array, isSorted, iteratee, thisArg);
122
```
123
124
**Usage Examples:**
125
126
```javascript
127
var uniq = require('lodash.uniq');
128
129
uniq([2, 1, 2]);
130
// => [2, 1]
131
132
// Sort first for better performance
133
uniq([1, 2, 2, 3], true);
134
// => [1, 2, 3]
135
136
// Custom iteratee
137
uniq([2.1, 1.2, 2.3], function(n) { return Math.floor(n); });
138
// => [2.1, 1.2]
139
```
140
141
### Difference
142
143
Creates an array of array values not included in the other given arrays.
144
145
```javascript { .api }
146
/**
147
* Creates an array of `array` values not included in the other given arrays
148
* using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
149
* for equality comparisons.
150
*
151
* @param {Array} array The array to inspect.
152
* @param {...Array} [values] The values to exclude.
153
* @returns {Array} Returns the new array of filtered values.
154
*/
155
function difference(array, ...values);
156
```
157
158
**Usage Examples:**
159
160
```javascript
161
var difference = require('lodash.difference');
162
163
difference([1, 2, 3], [4, 2]);
164
// => [1, 3]
165
166
difference([1, 2, 3, 4, 5], [2, 4], [1]);
167
// => [3, 5]
168
```
169
170
### Intersection
171
172
Creates an array of unique values that are included in all given arrays.
173
174
```javascript { .api }
175
/**
176
* Creates an array of unique values that are included in all given arrays
177
* using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
178
* for equality comparisons.
179
*
180
* @param {...Array} [arrays] The arrays to inspect.
181
* @returns {Array} Returns the new array of shared values.
182
*/
183
function intersection(...arrays);
184
```
185
186
**Usage Examples:**
187
188
```javascript
189
var intersection = require('lodash.intersection');
190
191
intersection([1, 2], [2, 3]);
192
// => [2]
193
194
intersection([1, 2, 3], [2, 3, 4], [2, 5]);
195
// => [2]
196
```
197
198
### Union
199
200
Creates an array of unique values, in order, from all given arrays.
201
202
```javascript { .api }
203
/**
204
* Creates an array of unique values, in order, from all given arrays using
205
* [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
206
* for equality comparisons.
207
*
208
* @param {...Array} [arrays] The arrays to inspect.
209
* @returns {Array} Returns the new array of combined values.
210
*/
211
function union(...arrays);
212
```
213
214
**Usage Examples:**
215
216
```javascript
217
var union = require('lodash.union');
218
219
union([1, 2], [2, 3]);
220
// => [1, 2, 3]
221
```
222
223
### Drop
224
225
Creates a slice of array with n elements dropped from the beginning.
226
227
```javascript { .api }
228
/**
229
* Creates a slice of `array` with `n` elements dropped from the beginning.
230
*
231
* @param {Array} array The array to query.
232
* @param {number} [n=1] The number of elements to drop.
233
* @returns {Array} Returns the slice of `array`.
234
*/
235
function drop(array, n);
236
```
237
238
**Usage Examples:**
239
240
```javascript
241
var drop = require('lodash.drop');
242
243
drop([1, 2, 3]);
244
// => [2, 3]
245
246
drop([1, 2, 3], 2);
247
// => [3]
248
```
249
250
### Take
251
252
Creates a slice of array with n elements taken from the beginning.
253
254
```javascript { .api }
255
/**
256
* Creates a slice of `array` with `n` elements taken from the beginning.
257
*
258
* @param {Array} array The array to query.
259
* @param {number} [n=1] The number of elements to take.
260
* @returns {Array} Returns the slice of `array`.
261
*/
262
function take(array, n);
263
```
264
265
**Usage Examples:**
266
267
```javascript
268
var take = require('lodash.take');
269
270
take([1, 2, 3]);
271
// => [1]
272
273
take([1, 2, 3], 2);
274
// => [1, 2]
275
```
276
277
### IndexOf
278
279
Gets the index at which the first occurrence of value is found in array.
280
281
```javascript { .api }
282
/**
283
* Gets the index at which the first occurrence of `value` is found in `array`
284
* using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
285
* for equality comparisons. If `fromIndex` is negative, it's used as the offset
286
* from the end of `array`. If `array` is sorted providing `true` for `fromIndex`
287
* performs a faster binary search.
288
*
289
* @param {Array} array The array to search.
290
* @param {*} value The value to search for.
291
* @param {boolean|number} [fromIndex=0] The index to search from or `true` to perform a binary search.
292
* @returns {number} Returns the index of the matched value, else `-1`.
293
*/
294
function indexOf(array, value, fromIndex);
295
```
296
297
**Usage Examples:**
298
299
```javascript
300
var indexOf = require('lodash.indexof');
301
302
indexOf([1, 2, 1, 2], 2);
303
// => 1
304
305
// Search from index
306
indexOf([1, 2, 1, 2], 2, 2);
307
// => 3
308
309
// Binary search on sorted array
310
indexOf([1, 1, 2, 2], 2, true);
311
// => 2
312
```
313
314
### Zip
315
316
Creates an array of grouped elements, the first of which contains the first elements of the given arrays.
317
318
```javascript { .api }
319
/**
320
* Creates an array of grouped elements, the first of which contains the first
321
* elements of the given arrays, the second of which contains the second elements
322
* of the given arrays, and so on.
323
*
324
* @param {...Array} [arrays] The arrays to process.
325
* @returns {Array} Returns the new array of grouped elements.
326
*/
327
function zip(...arrays);
328
```
329
330
**Usage Examples:**
331
332
```javascript
333
var zip = require('lodash.zip');
334
335
zip(['fred', 'barney'], [30, 40], [true, false]);
336
// => [['fred', 30, true], ['barney', 40, false]]
337
```
338
339
## Additional Functions
340
341
The array category also includes: `dropRight`, `dropRightWhile`, `dropWhile`, `fill`, `findIndex`, `findLastIndex`, `first`, `initial`, `last`, `lastIndexOf`, `pull`, `pullAt`, `remove`, `rest`, `slice`, `sortedIndex`, `sortedLastIndex`, `takeRight`, `takeRightWhile`, `takeWhile`, `unzip`, `without`, `xor`, `zipObject`
342
343
**Package Installation:**
344
345
```bash
346
npm install lodash.chunk lodash.compact lodash.flatten lodash.flattendeep
347
npm install lodash.uniq lodash.difference lodash.intersection lodash.union
348
npm install lodash.drop lodash.take lodash.indexof lodash.zip
349
```