- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.9.x
- Description
- A comprehensive JavaScript utility library with 296+ functions for arrays, objects, strings, and functional programming
- Author
- tessl
- Last updated
collection-methods.md docs/
1# Collection Methods23Iteration and manipulation methods that work on arrays, objects, and other iterable collections. These methods provide a consistent interface for working with different collection types.45## Capabilities67### At89Creates an array of values corresponding to paths of object.1011```javascript { .api }12/**13* Creates an array of values corresponding to `paths` of `object`.14* @param object - The object to iterate over15* @param paths - The property paths to pick16* @returns Returns the picked values17*/18function at<T>(object: T, paths: Array<string | number>): any[];19```2021### Count By2223Creates an object composed of keys generated from the results of running each element through iteratee.2425```javascript { .api }26/**27* Creates an object composed of keys generated from the results of running28* each element of `collection` thru `iteratee`. The corresponding value of29* each key is the number of times the key was returned by `iteratee`.30* @param collection - The collection to iterate over31* @param iteratee - The iteratee to transform keys32* @returns Returns the composed aggregate object33*/34function countBy<T>(35collection: T[],36iteratee?: string | ((value: T) => any)37): Record<string, number>;38```3940### Every4142Checks if predicate returns truthy for all elements of collection.4344```javascript { .api }45/**46* Checks if `predicate` returns truthy for **all** elements of `collection`.47* Iteration is stopped once `predicate` returns falsey.48* @param collection - The collection to iterate over49* @param predicate - The function invoked per iteration50* @returns Returns `true` if all elements pass the predicate check, else `false`51*/52function every<T>(53collection: T[],54predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean)55): boolean;56```5758### Filter5960Iterates over elements of collection, returning an array of all elements predicate returns truthy for.6162```javascript { .api }63/**64* Iterates over elements of `collection`, returning an array of all elements65* `predicate` returns truthy for.66* @param collection - The collection to iterate over67* @param predicate - The function invoked per iteration68* @returns Returns the new filtered array69*/70function filter<T>(71collection: T[],72predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean)73): T[];74```7576### Find7778Iterates over elements of collection, returning the first element predicate returns truthy for.7980```javascript { .api }81/**82* Iterates over elements of `collection`, returning the first element83* `predicate` returns truthy for.84* @param collection - The collection to inspect85* @param predicate - The function invoked per iteration86* @param fromIndex - The index to search from87* @returns Returns the matched element, else `undefined`88*/89function find<T>(90collection: T[],91predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean),92fromIndex?: number93): T | undefined;94```9596### Find Last9798Like find except that it iterates over elements from right to left.99100```javascript { .api }101/**102* This method is like `find` except that it iterates over elements of103* `collection` from right to left.104* @param collection - The collection to inspect105* @param predicate - The function invoked per iteration106* @param fromIndex - The index to search from107* @returns Returns the matched element, else `undefined`108*/109function findLast<T>(110collection: T[],111predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean),112fromIndex?: number113): T | undefined;114```115116### Flat Map117118Creates a flattened array of values by running each element in collection thru iteratee and flattening the mapped results.119120```javascript { .api }121/**122* Creates a flattened array of values by running each element in `collection`123* thru `iteratee` and flattening the mapped results. The iteratee is invoked124* with three arguments: (value, index|key, collection).125* @param collection - The collection to iterate over126* @param iteratee - The function invoked per iteration127* @returns Returns the new flattened array128*/129function flatMap<T, R>(130collection: T[],131iteratee?: string | ((value: T, index: number, collection: T[]) => R | R[])132): R[];133```134135### Flat Map Deep136137Like flatMap except that it recursively flattens the mapped results.138139```javascript { .api }140/**141* This method is like `flatMap` except that it recursively flattens the142* mapped results.143* @param collection - The collection to iterate over144* @param iteratee - The function invoked per iteration145* @returns Returns the new flattened array146*/147function flatMapDeep<T, R>(148collection: T[],149iteratee?: string | ((value: T, index: number, collection: T[]) => R | R[])150): R[];151```152153### Flat Map Depth154155Like flatMap except that it recursively flattens the mapped results up to depth times.156157```javascript { .api }158/**159* This method is like `flatMap` except that it recursively flattens the160* mapped results up to `depth` times.161* @param collection - The collection to iterate over162* @param iteratee - The function invoked per iteration163* @param depth - The maximum recursion depth164* @returns Returns the new flattened array165*/166function flatMapDepth<T, R>(167collection: T[],168iteratee?: string | ((value: T, index: number, collection: T[]) => R | R[]),169depth?: number170): R[];171```172173### For Each174175Iterates over elements of collection and invokes iteratee for each element.176177```javascript { .api }178/**179* Iterates over elements of `collection` and invokes `iteratee` for each element.180* The iteratee is invoked with three arguments: (value, index|key, collection).181* Iteratee functions may exit iteration early by explicitly returning `false`.182* @param collection - The collection to iterate over183* @param iteratee - The function invoked per iteration184* @returns Returns `collection`185*/186function forEach<T>(187collection: T[],188iteratee?: (value: T, index: number, collection: T[]) => any189): T[];190191function forEach<T>(192collection: Record<string, T>,193iteratee?: (value: T, key: string, collection: Record<string, T>) => any194): Record<string, T>;195```196197### For Each Right198199Like forEach except that it iterates over elements from right to left.200201```javascript { .api }202/**203* This method is like `forEach` except that it iterates over elements of204* `collection` from right to left.205* @param collection - The collection to iterate over206* @param iteratee - The function invoked per iteration207* @returns Returns `collection`208*/209function forEachRight<T>(210collection: T[],211iteratee?: (value: T, index: number, collection: T[]) => any212): T[];213214function forEachRight<T>(215collection: Record<string, T>,216iteratee?: (value: T, key: string, collection: Record<string, T>) => any217): Record<string, T>;218```219220### Group By221222Creates an object composed of keys generated from the results of running each element through iteratee.223224```javascript { .api }225/**226* Creates an object composed of keys generated from the results of running227* each element of `collection` thru `iteratee`. The order of grouped values228* is determined by the order they occur in `collection`.229* @param collection - The collection to iterate over230* @param iteratee - The iteratee to transform keys231* @returns Returns the composed aggregate object232*/233function groupBy<T>(234collection: T[],235iteratee?: string | ((value: T) => any)236): Record<string, T[]>;237```238239### Includes240241Checks if value is in collection.242243```javascript { .api }244/**245* Checks if `value` is in `collection`. If `collection` is a string, it's246* checked for a substring of `value`, otherwise SameValueZero is used for247* equality comparisons.248* @param collection - The collection to inspect249* @param value - The value to search for250* @param fromIndex - The index to search from251* @returns Returns `true` if `value` is found, else `false`252*/253function includes<T>(collection: T[], value: T, fromIndex?: number): boolean;254function includes(collection: string, value: string, fromIndex?: number): boolean;255function includes<T>(collection: Record<string, T>, value: T): boolean;256```257258### Invoke Map259260Invokes the method at path of each element in collection, returning an array of the results of each invoked method.261262```javascript { .api }263/**264* Invokes the method at `path` of each element in `collection`, returning265* an array of the results of each invoked method.266* @param collection - The collection to iterate over267* @param path - The path of the method to invoke268* @param args - The arguments to invoke each method with269* @returns Returns the array of results270*/271function invokeMap<T>(272collection: T[],273path: string | ((value: T) => any),274...args: any[]275): any[];276```277278### Key By279280Creates an object composed of keys generated from the results of running each element through iteratee.281282```javascript { .api }283/**284* Creates an object composed of keys generated from the results of running285* each element of `collection` thru `iteratee`. The corresponding value of286* each key is the last element responsible for generating the key.287* @param collection - The collection to iterate over288* @param iteratee - The iteratee to transform keys289* @returns Returns the composed aggregate object290*/291function keyBy<T>(292collection: T[],293iteratee?: string | ((value: T) => any)294): Record<string, T>;295```296297### Map298299Creates an array of values by running each element in collection thru iteratee.300301```javascript { .api }302/**303* Creates an array of values by running each element in `collection` thru304* `iteratee`. The iteratee is invoked with three arguments:305* (value, index|key, collection).306* @param collection - The collection to iterate over307* @param iteratee - The function invoked per iteration308* @returns Returns the new mapped array309*/310function map<T, R>(311collection: T[],312iteratee?: string | ((value: T, index: number, collection: T[]) => R)313): R[];314315function map<T, R>(316collection: Record<string, T>,317iteratee?: string | ((value: T, key: string, collection: Record<string, T>) => R)318): R[];319```320321### Order By322323Creates an array of elements, sorted in ascending order by the results of running each element thru each iteratee.324325```javascript { .api }326/**327* This method is like `sortBy` except that it allows specifying the sort328* orders of the iteratees to sort by. If `orders` is unspecified, all values329* are sorted in ascending order. Otherwise, specify an order of "desc" for330* descending or "asc" for ascending sort order of corresponding values.331* @param collection - The collection to iterate over332* @param iteratees - The iteratees to sort by333* @param orders - The sort orders of `iteratees`334* @returns Returns the new sorted array335*/336function orderBy<T>(337collection: T[],338iteratees?: Array<string | ((value: T) => any)>,339orders?: Array<'asc' | 'desc'>340): T[];341```342343### Partition344345Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for.346347```javascript { .api }348/**349* Creates an array of elements split into two groups, the first of which350* contains elements `predicate` returns truthy for, the second of which351* contains elements `predicate` returns falsey for.352* @param collection - The collection to iterate over353* @param predicate - The function invoked per iteration354* @returns Returns the array of grouped elements355*/356function partition<T>(357collection: T[],358predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean)359): [T[], T[]];360```361362### Reduce363364Reduces collection to a value which is the accumulated result of running each element thru iteratee.365366```javascript { .api }367/**368* Reduces `collection` to a value which is the accumulated result of running369* each element in `collection` thru `iteratee`, where each successive370* invocation is supplied the return value of the previous.371* @param collection - The collection to iterate over372* @param iteratee - The function invoked per iteration373* @param accumulator - The initial value374* @returns Returns the accumulated value375*/376function reduce<T, R>(377collection: T[],378iteratee: (accumulator: R, value: T, index: number, collection: T[]) => R,379accumulator?: R380): R;381382function reduce<T, R>(383collection: Record<string, T>,384iteratee: (accumulator: R, value: T, key: string, collection: Record<string, T>) => R,385accumulator?: R386): R;387```388389### Reduce Right390391Like reduce except that it iterates over elements from right to left.392393```javascript { .api }394/**395* This method is like `reduce` except that it iterates over elements of396* `collection` from right to left.397* @param collection - The collection to iterate over398* @param iteratee - The function invoked per iteration399* @param accumulator - The initial value400* @returns Returns the accumulated value401*/402function reduceRight<T, R>(403collection: T[],404iteratee: (accumulator: R, value: T, index: number, collection: T[]) => R,405accumulator?: R406): R;407408function reduceRight<T, R>(409collection: Record<string, T>,410iteratee: (accumulator: R, value: T, key: string, collection: Record<string, T>) => R,411accumulator?: R412): R;413```414415### Reject416417The opposite of filter; this method returns the elements of collection that predicate does not return truthy for.418419```javascript { .api }420/**421* The opposite of `filter`; this method returns the elements of `collection`422* that `predicate` does **not** return truthy for.423* @param collection - The collection to iterate over424* @param predicate - The function invoked per iteration425* @returns Returns the new filtered array426*/427function reject<T>(428collection: T[],429predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean)430): T[];431```432433### Sample434435Gets a random element from collection.436437```javascript { .api }438/**439* Gets a random element from `collection`.440* @param collection - The collection to sample441* @returns Returns the random element442*/443function sample<T>(collection: T[]): T | undefined;444function sample<T>(collection: Record<string, T>): T | undefined;445```446447### Sample Size448449Gets n random elements at unique keys from collection up to the size of collection.450451```javascript { .api }452/**453* Gets `n` random elements at unique keys from `collection` up to the454* size of `collection`.455* @param collection - The collection to sample456* @param n - The number of elements to sample457* @returns Returns the random elements458*/459function sampleSize<T>(collection: T[], n?: number): T[];460function sampleSize<T>(collection: Record<string, T>, n?: number): T[];461```462463### Shuffle464465Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.466467```javascript { .api }468/**469* Creates an array of shuffled values, using a version of the470* [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).471* @param collection - The collection to shuffle472* @returns Returns the new shuffled array473*/474function shuffle<T>(collection: T[]): T[];475function shuffle<T>(collection: Record<string, T>): T[];476```477478### Size479480Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.481482```javascript { .api }483/**484* Gets the size of `collection` by returning its length for array-like485* values or the number of own enumerable string keyed properties for objects.486* @param collection - The collection to inspect487* @returns Returns the collection size488*/489function size(collection: any[] | object | string): number;490```491492### Some493494Checks if predicate returns truthy for any element of collection.495496```javascript { .api }497/**498* Checks if `predicate` returns truthy for **any** element of `collection`.499* Iteration is stopped once `predicate` returns truthy.500* @param collection - The collection to iterate over501* @param predicate - The function invoked per iteration502* @returns Returns `true` if any element passes the predicate check, else `false`503*/504function some<T>(505collection: T[],506predicate?: string | object | ((value: T, index: number, collection: T[]) => boolean)507): boolean;508```509510### Sort By511512Creates an array of elements, sorted in ascending order by the results of running each element thru each iteratee.513514```javascript { .api }515/**516* Creates an array of elements, sorted in ascending order by the results517* of running each element in a collection thru each iteratee.518* @param collection - The collection to iterate over519* @param iteratees - The iteratees to sort by520* @returns Returns the new sorted array521*/522function sortBy<T>(523collection: T[],524...iteratees: Array<string | ((value: T) => any)>525): T[];526```527528## Aliases529530- `each` → `forEach`531- `eachRight` → `forEachRight`