- 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
collection.md docs/
1# Collection Processing23Iteration and processing methods for both arrays and objects, including mapping, filtering, grouping, and reduction operations. All collection methods work with arrays, objects, strings, maps, sets, and other iterables.45## Capabilities67### Iteration Methods89Core iteration utilities for processing collections.1011```javascript { .api }12/**13* Iterates over elements of `collection` and invokes `iteratee` for each element.14* The iteratee is invoked with three arguments: (value, index|key, collection).15*16* @param {Array|Object} collection The collection to iterate over17* @param {Function} [iteratee] The function invoked per iteration18* @returns {Array|Object} Returns `collection`19*/20function forEach(collection: Array|Object, iteratee?: Function): Array|Object;2122/**23* This method is like `forEach` except that it iterates over elements from right to left.24*25* @param {Array|Object} collection The collection to iterate over26* @param {Function} [iteratee] The function invoked per iteration27* @returns {Array|Object} Returns `collection`28*/29function forEachRight(collection: Array|Object, iteratee?: Function): Array|Object;30```3132### Transformation Methods3334Transform collections into new arrays or structures.3536```javascript { .api }37/**38* Creates an array of values by running each element in `collection` thru `iteratee`.39* The iteratee is invoked with three arguments: (value, index|key, collection).40*41* @param {Array|Object} collection The collection to iterate over42* @param {Function|Object|string} [iteratee] The function invoked per iteration43* @returns {Array} Returns the new mapped array44*/45function map(collection: Array|Object, iteratee?: Function|Object|string): Array;4647/**48* Creates a flattened array of values by running each element in `collection`49* thru `iteratee` and flattening the mapped results.50*51* @param {Array|Object} collection The collection to iterate over52* @param {Function|Object|string} [iteratee] The function invoked per iteration53* @returns {Array} Returns the new flattened array54*/55function flatMap(collection: Array|Object, iteratee?: Function|Object|string): Array;56```5758**Usage Examples:**5960```javascript61import { map, flatMap } from "lodash";6263// Basic mapping64map([1, 2, 3], n => n * 2);65// Result: [2, 4, 6]6667// Object mapping68map({ 'a': 1, 'b': 2 }, n => n * 2);69// Result: [2, 4]7071// Property shorthand72const users = [{ name: 'Alice' }, { name: 'Bob' }];73map(users, 'name');74// Result: ['Alice', 'Bob']7576// Flat mapping77flatMap([1, 2], n => [n, n]);78// Result: [1, 1, 2, 2]79```8081### Filtering Methods8283Filter collections based on predicates.8485```javascript { .api }86/**87* Iterates over elements of `collection`, returning an array of all elements88* `predicate` returns truthy for.89*90* @param {Array|Object} collection The collection to iterate over91* @param {Function|Object|string} [predicate] The function invoked per iteration92* @returns {Array} Returns the new filtered array93*/94function filter(collection: Array|Object, predicate?: Function|Object|string): Array;9596/**97* The opposite of `filter`; this method returns the elements of `collection`98* that `predicate` does **not** return truthy for.99*100* @param {Array|Object} collection The collection to iterate over101* @param {Function|Object|string} [predicate] The function invoked per iteration102* @returns {Array} Returns the new filtered array103*/104function reject(collection: Array|Object, predicate?: Function|Object|string): Array;105106/**107* Creates an array of two arrays, the first of which contains the elements of108* the given collection for which the predicate returns truthy, the second of which109* contains the elements for which the predicate returns falsy.110*111* @param {Array|Object} collection The collection to iterate over112* @param {Function|Object|string} [predicate] The function invoked per iteration113* @returns {Array} Returns the array of grouped elements114*/115function partition(collection: Array|Object, predicate?: Function|Object|string): Array[];116```117118### Search Methods119120Find elements in collections.121122```javascript { .api }123/**124* Iterates over elements of `collection`, returning the first element125* `predicate` returns truthy for.126*127* @param {Array|Object} collection The collection to inspect128* @param {Function|Object|string} [predicate] The function invoked per iteration129* @param {number} [fromIndex=0] The index to search from130* @returns {*} Returns the matched element, else `undefined`131*/132function find(collection: Array|Object, predicate?: Function|Object|string, fromIndex?: number): any;133134/**135* This method is like `find` except that it iterates over elements from right to left.136*137* @param {Array|Object} collection The collection to inspect138* @param {Function|Object|string} [predicate] The function invoked per iteration139* @param {number} [fromIndex=collection.length-1] The index to search from140* @returns {*} Returns the matched element, else `undefined`141*/142function findLast(collection: Array|Object, predicate?: Function|Object|string, fromIndex?: number): any;143144/**145* Checks if `value` is in `collection`. If `collection` is a string, it's146* checked for a substring of `value`.147*148* @param {Array|Object|string} collection The collection to inspect149* @param {*} value The value to search for150* @param {number} [fromIndex=0] The index to search from151* @returns {boolean} Returns `true` if `value` is found, else `false`152*/153function includes(collection: Array|Object|string, value: any, fromIndex?: number): boolean;154```155156### Testing Methods157158Test collections against predicates.159160```javascript { .api }161/**162* Checks if `predicate` returns truthy for **all** elements of `collection`.163* Iteration is stopped once `predicate` returns falsey.164*165* @param {Array|Object} collection The collection to iterate over166* @param {Function|Object|string} [predicate] The function invoked per iteration167* @returns {boolean} Returns `true` if all elements pass the predicate check, else `false`168*/169function every(collection: Array|Object, predicate?: Function|Object|string): boolean;170171/**172* Checks if `predicate` returns truthy for **any** element of `collection`.173* Iteration is stopped once `predicate` returns truthy.174*175* @param {Array|Object} collection The collection to iterate over176* @param {Function|Object|string} [predicate] The function invoked per iteration177* @returns {boolean} Returns `true` if any element passes the predicate check, else `false`178*/179function some(collection: Array|Object, predicate?: Function|Object|string): boolean;180```181182### Reduction Methods183184Reduce collections to single values.185186```javascript { .api }187/**188* Reduces `collection` to a value which is the accumulated result of running189* each element in `collection` thru `iteratee`.190*191* @param {Array|Object} collection The collection to iterate over192* @param {Function} [iteratee] The function invoked per iteration193* @param {*} [accumulator] The initial value194* @returns {*} Returns the accumulated value195*/196function reduce(collection: Array|Object, iteratee?: Function, accumulator?: any): any;197198/**199* This method is like `reduce` except that it iterates over elements from right to left.200*201* @param {Array|Object} collection The collection to iterate over202* @param {Function} [iteratee] The function invoked per iteration203* @param {*} [accumulator] The initial value204* @returns {*} Returns the accumulated value205*/206function reduceRight(collection: Array|Object, iteratee?: Function, accumulator?: any): any;207```208209**Usage Examples:**210211```javascript212import { reduce, reduceRight } from "lodash";213214// Sum array215reduce([1, 2, 3], (sum, n) => sum + n, 0);216// Result: 6217218// Group by property219reduce([220{ name: 'Alice', dept: 'IT' },221{ name: 'Bob', dept: 'HR' },222{ name: 'Charlie', dept: 'IT' }223], (result, person) => {224(result[person.dept] || (result[person.dept] = [])).push(person);225return result;226}, {});227// Result: { IT: [Alice, Charlie], HR: [Bob] }228229// Reduce from right230reduceRight([1, 2, 3, 4], (acc, n) => acc + n);231// Result: 10232```233234### Grouping Methods235236Group collections by criteria.237238```javascript { .api }239/**240* Creates an object composed of keys generated from the results of running241* each element of `collection` thru `iteratee`.242*243* @param {Array|Object} collection The collection to iterate over244* @param {Function|Object|string} [iteratee] The iteratee to transform keys245* @returns {Object} Returns the composed aggregate object246*/247function groupBy(collection: Array|Object, iteratee?: Function|Object|string): Object;248249/**250* Creates an object composed of keys generated from the results of running251* each element of `collection` thru `iteratee`. The corresponding value of252* each key is the last element responsible for generating the key.253*254* @param {Array|Object} collection The collection to iterate over255* @param {Function|Object|string} [iteratee] The iteratee to transform keys256* @returns {Object} Returns the composed aggregate object257*/258function keyBy(collection: Array|Object, iteratee?: Function|Object|string): Object;259260/**261* Creates an object composed of keys generated from the results of running262* each element of `collection` thru `iteratee`. The corresponding value of263* each key is the number of times the key was returned by `iteratee`.264*265* @param {Array|Object} collection The collection to iterate over266* @param {Function|Object|string} [iteratee] The iteratee to transform keys267* @returns {Object} Returns the composed aggregate object268*/269function countBy(collection: Array|Object, iteratee?: Function|Object|string): Object;270```271272**Usage Examples:**273274```javascript275import { groupBy, keyBy, countBy } from "lodash";276277const users = [278{ name: 'Alice', age: 25, active: true },279{ name: 'Bob', age: 30, active: false },280{ name: 'Charlie', age: 25, active: true }281];282283// Group by property284groupBy(users, 'age');285// Result: { '25': [Alice, Charlie], '30': [Bob] }286287// Key by property (last element wins)288keyBy(users, 'age');289// Result: { '25': Charlie, '30': Bob }290291// Count by property292countBy(users, 'active');293// Result: { 'true': 2, 'false': 1 }294295// Group by computed value296groupBy(['one', 'two', 'three'], 'length');297// Result: { '3': ['one', 'two'], '5': ['three'] }298```299300### Sorting Methods301302Sort collections with custom criteria.303304```javascript { .api }305/**306* Creates an array of elements, sorted in ascending order by the results of307* running each element in a collection thru each iteratee.308*309* @param {Array|Object} collection The collection to iterate over310* @param {...(Function|Object|string)} [iteratees] The iteratees to sort by311* @returns {Array} Returns the new sorted array312*/313function sortBy(collection: Array|Object, ...iteratees: (Function|Object|string)[]): Array;314315/**316* This method is like `sortBy` except that it allows specifying the sort317* orders of the iteratees.318*319* @param {Array|Object} collection The collection to iterate over320* @param {Array|Function|Object|string} [iteratees] The iteratees to sort by321* @param {Array} [orders] The sort orders of `iteratees`322* @returns {Array} Returns the new sorted array323*/324function orderBy(collection: Array|Object, iteratees?: Array|Function|Object|string, orders?: Array): Array;325```326327### Sampling Methods328329Get random elements from collections.330331```javascript { .api }332/**333* Gets a random element from `collection`.334*335* @param {Array|Object} collection The collection to sample336* @returns {*} Returns the random element337*/338function sample(collection: Array|Object): any;339340/**341* Gets `n` random elements at unique keys from `collection` up to the342* size of `collection`.343*344* @param {Array|Object} collection The collection to sample345* @param {number} [n=1] The number of elements to sample346* @returns {Array} Returns the random elements347*/348function sampleSize(collection: Array|Object, n?: number): Array;349350/**351* Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.352*353* @param {Array|Object} collection The collection to shuffle354* @returns {Array} Returns the new shuffled array355*/356function shuffle(collection: Array|Object): Array;357```358359### Size Methods360361Get collection sizes.362363```javascript { .api }364/**365* Gets the size of `collection` by returning its length for array-like366* values or the number of own enumerable string keyed properties for objects.367*368* @param {Array|Object|string} collection The collection to inspect369* @returns {number} Returns the collection size370*/371function size(collection: Array|Object|string): number;372```373374### Invocation Methods375376Invoke methods on collection elements.377378```javascript { .api }379/**380* Invokes the method at `path` of each element in `collection`, returning381* an array of the results of each invoked method.382*383* @param {Array|Object} collection The collection to iterate over384* @param {Array|Function|string} path The path of the method to invoke or the function invoked per iteration385* @param {...*} [args] The arguments to invoke each method with386* @returns {Array} Returns the array of results387*/388function invokeMap(collection: Array|Object, path: Array|Function|string, ...args: any[]): Array;389```