- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.1.x
- Description
- A comprehensive JavaScript utility library delivering modularity, performance and extra features for arrays, objects, functions and more
- Author
- tessl
- Last updated
array.md docs/
1# Array Processing23Comprehensive array manipulation functions for chunking, flattening, deduplication, set operations, filtering, and transformations. Lodash provides 63 specialized array functions for efficient array processing.45## Capabilities67### Array Chunking89Creates arrays of elements split into groups of specified sizes.1011```javascript { .api }12/**13* Creates an array of elements split into groups the length of `size`.14* If `array` can't be split evenly, the final chunk will be the remaining elements.15* @param {Array} array - The array to process16* @param {number} [size=0] - The length of each chunk17* @returns {Array} Returns the new array containing chunks18*/19function chunk(array, size);20```2122**Usage Examples:**2324```javascript25const _ = require('lodash');2627_.chunk(['a', 'b', 'c', 'd'], 2);28// => [['a', 'b'], ['c', 'd']]2930_.chunk(['a', 'b', 'c', 'd'], 3);31// => [['a', 'b', 'c'], ['d']]3233_.chunk([1, 2, 3, 4, 5], 2);34// => [[1, 2], [3, 4], [5]]35```3637### Array Compacting3839Removes falsy values from arrays.4041```javascript { .api }42/**43* Creates an array with all falsey values removed. The values `false`, `null`,44* `0`, `""`, `undefined`, and `NaN` are falsey.45* @param {Array} array - The array to compact46* @returns {Array} Returns the new array of filtered values47*/48function compact(array);49```5051**Usage Examples:**5253```javascript54_.compact([0, 1, false, 2, '', 3]);55// => [1, 2, 3]5657_.compact([null, undefined, 'hello', 0, false, 'world']);58// => ['hello', 'world']59```6061### Array Differences6263Creates arrays of unique values not included in other provided arrays.6465```javascript { .api }66/**67* Creates an array of unique `array` values not included in the other provided arrays.68* @param {Array} array - The array to inspect69* @param {...Array} [values] - The values to exclude70* @returns {Array} Returns the new array of filtered values71*/72function difference(array, ...values);7374/**75* Like `difference` except that it accepts `iteratee` to compute criteria.76* @param {Array} array - The array to inspect77* @param {...Array} [values] - The values to exclude78* @param {Function} iteratee - The iteratee invoked per element79* @returns {Array} Returns the new array of filtered values80*/81function differenceBy(array, ...values, iteratee);8283/**84* Like `difference` except that it accepts `comparator` for element comparisons.85* @param {Array} array - The array to inspect86* @param {...Array} [values] - The values to exclude87* @param {Function} comparator - The comparator invoked per element88* @returns {Array} Returns the new array of filtered values89*/90function differenceWith(array, ...values, comparator);91```9293**Usage Examples:**9495```javascript96_.difference([3, 2, 1], [4, 2]);97// => [3, 1]9899_.differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor);100// => [3.1, 1.3]101102_.differenceWith([{ x: 1 }, { x: 2 }], [{ x: 1 }], _.isEqual);103// => [{ x: 2 }]104```105106### Array Flattening107108Flattens arrays to specified depth levels.109110```javascript { .api }111/**112* Flattens `array` a single level.113* @param {Array} array - The array to flatten114* @returns {Array} Returns the new flattened array115*/116function flatten(array);117118/**119* Recursively flattens `array`.120* @param {Array} array - The array to flatten121* @returns {Array} Returns the new flattened array122*/123function flattenDeep(array);124125/**126* Recursively flatten `array` up to `depth` times.127* @param {Array} array - The array to flatten128* @param {number} [depth=1] - The maximum recursion depth129* @returns {Array} Returns the new flattened array130*/131function flattenDepth(array, depth);132```133134**Usage Examples:**135136```javascript137_.flatten([1, [2, 3, [4]]]);138// => [1, 2, 3, [4]]139140_.flattenDeep([1, [2, 3, [4, [5]]]]);141// => [1, 2, 3, 4, 5]142143_.flattenDepth([1, [2, [3, [4]], 5]], 2);144// => [1, 2, 3, [4], 5]145```146147### Array Deduplication148149Creates duplicate-free versions of arrays.150151```javascript { .api }152/**153* Creates a duplicate-free version of an array.154* @param {Array} array - The array to inspect155* @returns {Array} Returns the new duplicate free array156*/157function uniq(array);158159/**160* Like `uniq` except that it accepts `iteratee` to compute uniqueness criteria.161* @param {Array} array - The array to inspect162* @param {Function} iteratee - The iteratee invoked per element163* @returns {Array} Returns the new duplicate free array164*/165function uniqBy(array, iteratee);166167/**168* Like `uniq` except that it accepts `comparator` for element comparisons.169* @param {Array} array - The array to inspect170* @param {Function} comparator - The comparator invoked per element171* @returns {Array} Returns the new duplicate free array172*/173function uniqWith(array, comparator);174```175176**Usage Examples:**177178```javascript179_.uniq([2, 1, 2]);180// => [2, 1]181182_.uniqBy([2.1, 1.2, 2.3], Math.floor);183// => [2.1, 1.2]184185_.uniqWith([{ x: 1 }, { x: 2 }, { x: 1 }], _.isEqual);186// => [{ x: 1 }, { x: 2 }]187```188189### Array Access and Slicing190191Functions for accessing and extracting portions of arrays.192193```javascript { .api }194/**195* Gets the first element of `array`.196* @param {Array} array - The array to query197* @returns {*} Returns the first element of `array`198*/199function head(array);200201/**202* Gets the last element of `array`.203* @param {Array} array - The array to query204* @returns {*} Returns the last element of `array`205*/206function last(array);207208/**209* Creates a slice of `array` with `n` elements taken from the beginning.210* @param {Array} array - The array to query211* @param {number} [n=1] - The number of elements to take212* @returns {Array} Returns the slice of `array`213*/214function take(array, n);215216/**217* Creates a slice of `array` with `n` elements dropped from the beginning.218* @param {Array} array - The array to query219* @param {number} [n=1] - The number of elements to drop220* @returns {Array} Returns the slice of `array`221*/222function drop(array, n);223```224225**Usage Examples:**226227```javascript228_.head([1, 2, 3]);229// => 1230231_.last([1, 2, 3]);232// => 3233234_.take([1, 2, 3, 4, 5], 3);235// => [1, 2, 3]236237_.drop([1, 2, 3, 4, 5], 2);238// => [3, 4, 5]239```240241### Set Operations242243Array functions for intersection, union, and symmetric difference operations.244245```javascript { .api }246/**247* Creates an array of unique values that are included in all given arrays.248* @param {...Array} [arrays] - The arrays to inspect249* @returns {Array} Returns the new array of intersecting values250*/251function intersection(...arrays);252253/**254* Creates an array of unique values from all given arrays.255* @param {...Array} [arrays] - The arrays to inspect256* @returns {Array} Returns the new array of combined values257*/258function union(...arrays);259260/**261* Creates an array of unique values that is the symmetric difference of the given arrays.262* @param {...Array} [arrays] - The arrays to inspect263* @returns {Array} Returns the new array of values264*/265function xor(...arrays);266```267268**Usage Examples:**269270```javascript271_.intersection([2, 1], [4, 2], [1, 2]);272// => [2]273274_.union([2], [1, 2]);275// => [2, 1]276277_.xor([2, 1], [4, 2]);278// => [1, 4]279```280281### Array Searching and Indexing282283Functions for finding elements and their positions in arrays.284285```javascript { .api }286/**287* Gets the index at which the first occurrence of `value` is found in `array`.288* @param {Array} array - The array to search289* @param {*} value - The value to search for290* @param {number} [fromIndex=0] - The index to search from291* @returns {number} Returns the index of the matched value, else -1292*/293function indexOf(array, value, fromIndex);294295/**296* This method is like `indexOf` except that it searches from right to left.297* @param {Array} array - The array to search298* @param {*} value - The value to search for299* @param {number} [fromIndex=array.length-1] - The index to search from300* @returns {number} Returns the index of the matched value, else -1301*/302function lastIndexOf(array, value, fromIndex);303304/**305* Returns the index of the first element `predicate` returns truthy for.306* @param {Array} array - The array to search307* @param {Function} predicate - The function invoked per iteration308* @param {number} [fromIndex=0] - The index to search from309* @returns {number} Returns the index of the found element, else -1310*/311function findIndex(array, predicate, fromIndex);312```313314**Usage Examples:**315316```javascript317_.indexOf([1, 2, 1, 2], 2);318// => 1319320_.lastIndexOf([1, 2, 1, 2], 2);321// => 3322323_.findIndex([{ user: 'fred' }, { user: 'barney' }], { user: 'barney' });324// => 1325```326327### Array Zipping and Unzipping328329Functions for combining and separating arrays.330331```javascript { .api }332/**333* Creates an array of grouped elements.334* @param {...Array} [arrays] - The arrays to process335* @returns {Array} Returns the new array of grouped elements336*/337function zip(...arrays);338339/**340* This method is like `zip` except that it accepts an array of grouped elements.341* @param {Array} array - The array of grouped elements to process342* @returns {Array} Returns the new array of regrouped elements343*/344function unzip(array);345346/**347* Like `zip` except that it accepts `iteratee` to specify how grouped values should be combined.348* @param {...Array} [arrays] - The arrays to process349* @param {Function} iteratee - The function to combine grouped values350* @returns {Array} Returns the new array of combined values351*/352function zipWith(...arrays, iteratee);353```354355**Usage Examples:**356357```javascript358_.zip(['fred', 'barney'], [30, 40], [true, false]);359// => [['fred', 30, true], ['barney', 40, false]]360361_.unzip([['fred', 30, true], ['barney', 40, false]]);362// => [['fred', 'barney'], [30, 40], [true, false]]363364_.zipWith([1, 2], [10, 20], [100, 200], (a, b, c) => a + b + c);365// => [111, 222]366```367368## Complete Array Function List369370Lodash provides these 63 array functions:371372**Core Functions:** `chunk`, `compact`, `concat`, `difference`, `differenceBy`, `differenceWith`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `fill`, `findIndex`, `findLastIndex`, `flatten`, `flattenDeep`, `flattenDepth`, `fromPairs`, `head`, `indexOf`, `initial`, `intersection`, `intersectionBy`, `intersectionWith`, `join`, `last`, `lastIndexOf`, `nth`, `pull`, `pullAll`, `pullAllBy`, `pullAllWith`, `pullAt`, `remove`, `reverse`, `slice`, `sortedIndex`, `sortedIndexBy`, `sortedIndexOf`, `sortedLastIndex`, `sortedLastIndexBy`, `sortedLastIndexOf`, `sortedUniq`, `sortedUniqBy`, `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, `uniqWith`, `unzip`, `unzipWith`, `without`, `xor`, `xorBy`, `xorWith`, `zip`, `zipObject`, `zipObjectDeep`, `zipWith`373374**Aliases:** `first` (alias for `head`)375376Each function includes comprehensive parameter validation, optional iteratee support, and optimized performance for large arrays.