- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.6.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
array.md docs/
1# Array Operations23Comprehensive array manipulation utilities including transformation, filtering, set operations, and sorting functions. All array methods return new arrays without modifying the original.45## Capabilities67### Union Operations89Combines arrays into a single array with unique values, providing different comparison strategies.1011```javascript { .api }12/**13* Creates an array of unique values, in order, from all given arrays using14* SameValueZero for equality comparisons.15*16* @param {...Array} [arrays] The arrays to inspect17* @returns {Array} Returns the new array of combined values18*/19function union(...arrays: Array[]): Array;2021/**22* This method is like `union` except that it accepts `iteratee` which is23* invoked for each element of each `arrays` to generate the criterion by which24* uniqueness is computed. The iteratee is invoked with one argument: (value).25*26* @param {...Array} [arrays] The arrays to inspect27* @param {Function|Object|string} [iteratee] The iteratee invoked per element28* @returns {Array} Returns the new array of combined values29*/30function unionBy(...arrays: Array[], iteratee: Function|Object|string): Array;3132/**33* This method is like `union` except that it accepts `comparator` which34* is invoked to compare elements of `arrays`. The comparator is invoked35* with two arguments: (arrVal, othVal).36*37* @param {...Array} [arrays] The arrays to inspect38* @param {Function} [comparator] The comparator invoked per element39* @returns {Array} Returns the new array of combined values40*/41function unionWith(...arrays: Array[], comparator: Function): Array;42```4344**Usage Examples:**4546```javascript47import { union, unionBy, unionWith, isEqual } from "lodash";4849// Basic union - removes duplicates50union([2, 1], [4, 2], [1, 2]);51// Result: [2, 1, 4]5253// Union with iteratee - use Math.floor to compare54unionBy([2.1, 1.2], [4.3, 2.4], Math.floor);55// Result: [2.1, 1.2, 4.3]5657// Union with property shorthand58unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');59// Result: [{ 'x': 1 }, { 'x': 2 }]6061// Union with custom comparator62const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];63const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];64unionWith(objects, others, isEqual);65// Result: [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]66```6768### Array Chunking6970Splits arrays into smaller groups of specified size.7172```javascript { .api }73/**74* Creates an array of elements split into groups the length of `size`.75* If `array` can't be split evenly, the final chunk will be the remaining elements.76*77* @param {Array} array The array to process78* @param {number} [size=1] The length of each chunk79* @returns {Array} Returns the new array of chunks80*/81function chunk(array: Array, size?: number): Array[];82```8384### Array Compacting8586Removes falsey values from arrays.8788```javascript { .api }89/**90* Creates an array with all falsey values removed. The values `false`, `null`,91* `0`, `""`, `undefined`, and `NaN` are falsey.92*93* @param {Array} array The array to compact94* @returns {Array} Returns the new array of filtered values95*/96function compact(array: Array): Array;97```9899### Array Concatenation100101Combines arrays and values into a new array.102103```javascript { .api }104/**105* Creates a new array concatenating `array` with any additional arrays106* and/or values.107*108* @param {Array} array The array to concatenate109* @param {...*} [values] The values to concatenate110* @returns {Array} Returns the new concatenated array111*/112function concat(array: Array, ...values: any[]): Array;113```114115### Difference Operations116117Creates arrays of values not included in other arrays.118119```javascript { .api }120/**121* Creates an array of `array` values not included in the other given arrays122* using SameValueZero for equality comparisons.123*124* @param {Array} array The array to inspect125* @param {...Array} [values] The values to exclude126* @returns {Array} Returns the new array of filtered values127*/128function difference(array: Array, ...values: Array[]): Array;129130/**131* This method is like `difference` except that it accepts `iteratee` which132* is invoked for each element of `array` and `values` to generate the criterion133* by which they're compared.134*135* @param {Array} array The array to inspect136* @param {...Array} [values] The values to exclude137* @param {Function|Object|string} [iteratee] The iteratee invoked per element138* @returns {Array} Returns the new array of filtered values139*/140function differenceBy(array: Array, ...values: Array[], iteratee: Function|Object|string): Array;141142/**143* This method is like `difference` except that it accepts `comparator`144* which is invoked to compare elements of `array` to `values`.145*146* @param {Array} array The array to inspect147* @param {...Array} [values] The values to exclude148* @param {Function} [comparator] The comparator invoked per element149* @returns {Array} Returns the new array of filtered values150*/151function differenceWith(array: Array, ...values: Array[], comparator: Function): Array;152```153154### Drop Operations155156Creates slices of arrays with elements dropped from the beginning or end.157158```javascript { .api }159/**160* Creates a slice of `array` with `n` elements dropped from the beginning.161*162* @param {Array} array The array to query163* @param {number} [n=1] The number of elements to drop164* @returns {Array} Returns the slice of `array`165*/166function drop(array: Array, n?: number): Array;167168/**169* Creates a slice of `array` with `n` elements dropped from the end.170*171* @param {Array} array The array to query172* @param {number} [n=1] The number of elements to drop173* @returns {Array} Returns the slice of `array`174*/175function dropRight(array: Array, n?: number): Array;176177/**178* Creates a slice of `array` excluding elements dropped from the beginning.179* Elements are dropped until `predicate` returns falsey.180*181* @param {Array} array The array to query182* @param {Function|Object|string} [predicate] The function invoked per iteration183* @returns {Array} Returns the slice of `array`184*/185function dropWhile(array: Array, predicate?: Function|Object|string): Array;186187/**188* Creates a slice of `array` excluding elements dropped from the end.189* Elements are dropped until `predicate` returns falsey.190*191* @param {Array} array The array to query192* @param {Function|Object|string} [predicate] The function invoked per iteration193* @returns {Array} Returns the slice of `array`194*/195function dropRightWhile(array: Array, predicate?: Function|Object|string): Array;196```197198### Array Filling199200Fills elements of arrays with a specified value.201202```javascript { .api }203/**204* Fills elements of `array` with `value` from `start` up to, but not205* including, `end`.206*207* @param {Array} array The array to fill208* @param {*} value The value to fill `array` with209* @param {number} [start=0] The start position210* @param {number} [end=array.length] The end position211* @returns {Array} Returns `array`212*/213function fill(array: Array, value: any, start?: number, end?: number): Array;214```215216### Index Finding217218Finds indexes of elements in arrays.219220```javascript { .api }221/**222* This method is like `find` except that it returns the index of the first223* element `predicate` returns truthy for instead of the element itself.224*225* @param {Array} array The array to inspect226* @param {Function|Object|string} [predicate] The function invoked per iteration227* @param {number} [fromIndex=0] The index to search from228* @returns {number} Returns the index of the found element, else -1229*/230function findIndex(array: Array, predicate?: Function|Object|string, fromIndex?: number): number;231232/**233* This method is like `findIndex` except that it iterates over elements234* from right to left.235*236* @param {Array} array The array to inspect237* @param {Function|Object|string} [predicate] The function invoked per iteration238* @param {number} [fromIndex=array.length-1] The index to search from239* @returns {number} Returns the index of the found element, else -1240*/241function findLastIndex(array: Array, predicate?: Function|Object|string, fromIndex?: number): number;242```243244### Array Flattening245246Flattens nested arrays to specified depths.247248```javascript { .api }249/**250* Flattens `array` a single level deep.251*252* @param {Array} array The array to flatten253* @returns {Array} Returns the new flattened array254*/255function flatten(array: Array): Array;256257/**258* Recursively flattens `array`.259*260* @param {Array} array The array to flatten261* @returns {Array} Returns the new flattened array262*/263function flattenDeep(array: Array): Array;264265/**266* Recursively flatten `array` up to `depth` times.267*268* @param {Array} array The array to flatten269* @param {number} [depth=1] The maximum recursion depth270* @returns {Array} Returns the new flattened array271*/272function flattenDepth(array: Array, depth?: number): Array;273```274275### Intersection Operations276277Creates arrays of unique values included in all given arrays.278279```javascript { .api }280/**281* Creates an array of unique values that are included in all given arrays282* using SameValueZero for equality comparisons.283*284* @param {...Array} [arrays] The arrays to inspect285* @returns {Array} Returns the new array of intersecting values286*/287function intersection(...arrays: Array[]): Array;288289/**290* This method is like `intersection` except that it accepts `iteratee`291* which is invoked for each element of each `arrays` to generate the criterion292* by which they're compared.293*294* @param {...Array} [arrays] The arrays to inspect295* @param {Function|Object|string} [iteratee] The iteratee invoked per element296* @returns {Array} Returns the new array of intersecting values297*/298function intersectionBy(...arrays: Array[], iteratee: Function|Object|string): Array;299300/**301* This method is like `intersection` except that it accepts `comparator`302* which is invoked to compare elements of `arrays`.303*304* @param {...Array} [arrays] The arrays to inspect305* @param {Function} [comparator] The comparator invoked per element306* @returns {Array} Returns the new array of intersecting values307*/308function intersectionWith(...arrays: Array[], comparator: Function): Array;309```310311### Basic Array Operations312313Core array methods for searching and converting arrays.314315```javascript { .api }316/**317* Gets the index at which the first occurrence of `value` is found in `array` using SameValueZero for equality comparisons. If `fromIndex` is negative, it's used as the offset from the end of `array`.318*319* @param {Array} array The array to inspect320* @param {*} value The value to search for321* @param {number} [fromIndex=0] The index to search from322* @returns {number} Returns the index of the matched value, else `-1`323*/324function indexOf(array: Array, value: any, fromIndex?: number): number;325326/**327* This method is like `indexOf` except that it iterates over elements of `array` from right to left.328*329* @param {Array} array The array to inspect330* @param {*} value The value to search for331* @param {number} [fromIndex=array.length-1] The index to search from332* @returns {number} Returns the index of the matched value, else `-1`333*/334function lastIndexOf(array: Array, value: any, fromIndex?: number): number;335336/**337* Converts all elements in `array` into a string separated by `separator`.338*339* @param {Array} array The array to convert340* @param {string} [separator=','] The element separator341* @returns {string} Returns the joined string342*/343function join(array: Array, separator?: string): string;344```345346### Array Access347348Gets elements from specific positions in arrays.349350```javascript { .api }351/**352* Gets the first element of `array`.353*354* @param {Array} array The array to query355* @returns {*} Returns the first element of `array`356*/357function head(array: Array): any;358359/**360* Gets the last element of `array`.361*362* @param {Array} array The array to query363* @returns {*} Returns the last element of `array`364*/365function last(array: Array): any;366367/**368* Gets all but the first element of `array`.369*370* @param {Array} array The array to query371* @returns {Array} Returns the slice of `array`372*/373function tail(array: Array): Array;374375/**376* Gets all but the last element of `array`.377*378* @param {Array} array The array to query379* @returns {Array} Returns the slice of `array`380*/381function initial(array: Array): Array;382383/**384* Creates an array of elements corresponding to the given keys, or indexes, of `collection`.385*386* @param {Object} object The object to iterate over387* @param {...(string|string[])} [paths] The property paths to pick388* @returns {Array} Returns the picked values389*/390function at(object: Object, ...paths: Array<string|string[]>): Array;391```392393### Unique Operations394395Creates duplicate-free versions of arrays.396397```javascript { .api }398/**399* Creates a duplicate-free version of an array, using SameValueZero400* for equality comparisons, in which only the first occurrence of each element is kept.401*402* @param {Array} array The array to inspect403* @returns {Array} Returns the new duplicate free array404*/405function uniq(array: Array): Array;406407/**408* This method is like `uniq` except that it accepts `iteratee` which is409* invoked for each element in `array` to generate the criterion by which410* uniqueness is computed.411*412* @param {Array} array The array to inspect413* @param {Function|Object|string} [iteratee] The iteratee invoked per element414* @returns {Array} Returns the new duplicate free array415*/416function uniqBy(array: Array, iteratee?: Function|Object|string): Array;417418/**419* This method is like `uniq` except that it accepts `comparator` which420* is invoked to compare elements of `array`.421*422* @param {Array} array The array to inspect423* @param {Function} [comparator] The comparator invoked per element424* @returns {Array} Returns the new duplicate free array425*/426function uniqWith(array: Array, comparator?: Function): Array;427```428429### Array Exclusion430431Creates arrays excluding specified values.432433```javascript { .api }434/**435* Creates an array excluding all given values using SameValueZero436* for equality comparisons.437*438* @param {Array} array The array to inspect439* @param {...*} [values] The values to exclude440* @returns {Array} Returns the new array of filtered values441*/442function without(array: Array, ...values: any[]): Array;443```444445### Symmetric Difference446447Creates arrays of unique values that is the symmetric difference of given arrays.448449```javascript { .api }450/**451* Creates an array of unique values that is the symmetric difference of the given arrays.452* The order of result values is determined by the order they occur in the arrays.453*454* @param {...Array} [arrays] The arrays to inspect455* @returns {Array} Returns the new array of filtered values456*/457function xor(...arrays: Array[]): Array;458459/**460* This method is like `xor` except that it accepts `iteratee` which is461* invoked for each element of each `arrays` to generate the criterion by which462* by which they're compared.463*464* @param {...Array} [arrays] The arrays to inspect465* @param {Function|Object|string} [iteratee] The iteratee invoked per element466* @returns {Array} Returns the new array of filtered values467*/468function xorBy(...arrays: Array[], iteratee: Function|Object|string): Array;469470/**471* This method is like `xor` except that it accepts `comparator` which is472* invoked to compare elements of `arrays`.473*474* @param {...Array} [arrays] The arrays to inspect475* @param {Function} [comparator] The comparator invoked per element476* @returns {Array} Returns the new array of filtered values477*/478function xorWith(...arrays: Array[], comparator: Function): Array;479```480481### Array Zipping482483Creates arrays of grouped elements.484485```javascript { .api }486/**487* Creates an array of grouped elements, the first of which contains the first488* elements of the given arrays, the second of which contains the second489* elements of the given arrays, and so on.490*491* @param {...Array} [arrays] The arrays to process492* @returns {Array} Returns the new array of grouped elements493*/494function zip(...arrays: Array[]): Array[];495496/**497* This method is like `zip` except that it accepts `iteratee` to specify498* how grouped values should be combined.499*500* @param {...Array} [arrays] The arrays to process501* @param {Function} [iteratee] The function to combine grouped values502* @returns {Array} Returns the new array of grouped elements503*/504function zipWith(...arrays: Array[], iteratee: Function): Array;505506/**507* This method is like `zip` except that it accepts an array of grouped508* elements and creates an array regrouping the elements to their pre-zip configuration.509*510* @param {Array} array The array of grouped elements to process511* @returns {Array} Returns the new array of regrouped elements512*/513function unzip(array: Array): Array[];514515/**516* This method is like `unzip` except that it accepts `iteratee` to specify517* how regrouped values should be combined.518*519* @param {Array} array The array of grouped elements to process520* @param {Function} [iteratee] The function to combine regrouped values521* @returns {Array} Returns the new array of regrouped elements522*/523function unzipWith(array: Array, iteratee?: Function): Array;524525/**526* The inverse of `toPairs`; this method returns an object composed from key-value `pairs`.527*528* @param {Array} pairs The key-value pairs529* @returns {Object} Returns the new object530*/531function fromPairs(pairs: Array): Object;532533/**534* This method is like `fromPairs` except that it accepts two arrays,535* one of property identifiers and one of corresponding values.536*537* @param {Array} [props=[]] The property identifiers538* @param {Array} [values=[]] The property values539* @returns {Object} Returns the new object540*/541function zipObject(props?: Array, values?: Array): Object;542543/**544* This method is like `zipObject` except that it supports property paths.545*546* @param {Array} [props=[]] The property identifiers547* @param {Array} [values=[]] The property values548* @returns {Object} Returns the new object549*/550function zipObjectDeep(props?: Array, values?: Array): Object;551```