_.countBy(collection, [iteratee=_.identity])

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee. The iteratee is invoked with one argument: (value).

Since

0.5.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The iteratee to transform keys.

Returns

(Object): Returns the composed aggregate object.

Example

  1. _.countBy([6.1, 4.2, 6.3], Math.floor);
  2. // => { '4': 1, '6': 2 }
  3. // The `_.property` iteratee shorthand.
  4. _.countBy(['one', 'two', 'three'], 'length');
  5. // => { '3': 2, '5': 1 }

_.every(collection, [predicate=_.identity])

Checks if predicate returns truthy for all elements of collection. Iteration is stopped once predicate returns falsey. The predicate is invoked with three arguments: (value, index|key, collection).

Note: This method returns true for empty collections because everything is true of elements of empty collections.

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(boolean): Returns true if all elements pass the predicate check, else false.

Example

  1. _.every([true, 1, null, 'yes'], Boolean);
  2. // => false
  3. var users = [
  4. { 'user': 'barney', 'age': 36, 'active': false },
  5. { 'user': 'fred', 'age': 40, 'active': false }
  6. ];
  7. // The `_.matches` iteratee shorthand.
  8. _.every(users, { 'user': 'barney', 'active': false });
  9. // => false
  10. // The `_.matchesProperty` iteratee shorthand.
  11. _.every(users, ['active', false]);
  12. // => true
  13. // The `_.property` iteratee shorthand.
  14. _.every(users, 'active');
  15. // => false

_.filter(collection, [predicate=_.identity])

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

Note: Unlike _.remove, this method returns a new array.

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new filtered array.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': true },
  3. { 'user': 'fred', 'age': 40, 'active': false }
  4. ];
  5. _.filter(users, function(o) { return !o.active; });
  6. // => objects for ['fred']
  7. // The `_.matches` iteratee shorthand.
  8. _.filter(users, { 'age': 36, 'active': true });
  9. // => objects for ['barney']
  10. // The `_.matchesProperty` iteratee shorthand.
  11. _.filter(users, ['active', false]);
  12. // => objects for ['fred']
  13. // The `_.property` iteratee shorthand.
  14. _.filter(users, 'active');
  15. // => objects for ['barney']

_.find(collection, [predicate=_.identity], [fromIndex=0])

Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.
  3. [fromIndex=0] (number): The index to search from.

Returns

(*): Returns the matched element, else undefined.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': true },
  3. { 'user': 'fred', 'age': 40, 'active': false },
  4. { 'user': 'pebbles', 'age': 1, 'active': true }
  5. ];
  6. _.find(users, function(o) { return o.age < 40; });
  7. // => object for 'barney'
  8. // The `_.matches` iteratee shorthand.
  9. _.find(users, { 'age': 1, 'active': true });
  10. // => object for 'pebbles'
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.find(users, ['active', false]);
  13. // => object for 'fred'
  14. // The `_.property` iteratee shorthand.
  15. _.find(users, 'active');
  16. // => object for 'barney'

_.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])

This method is like _.find except that it iterates over elements of collection from right to left.

Since

2.0.0

Arguments

  1. collection (Array|Object): The collection to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.
  3. [fromIndex=collection.length-1] (number): The index to search from.

Returns

(*): Returns the matched element, else undefined.

Example

  1. _.findLast([1, 2, 3, 4], function(n) {
  2. return n % 2 == 1;
  3. });
  4. // => 3

_.flatMap(collection, [iteratee=_.identity])

Creates a flattened array of values by running each element in collection thru iteratee and flattening the mapped results. The iteratee is invoked with three arguments: (value, index|key, collection).

Since

4.0.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new flattened array.

Example

  1. function duplicate(n) {
  2. return [n, n];
  3. }
  4. _.flatMap([1, 2], duplicate);
  5. // => [1, 1, 2, 2]

_.flatMapDeep(collection, [iteratee=_.identity])

This method is like _.flatMap except that it recursively flattens the mapped results.

Since

4.7.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new flattened array.

Example

  1. function duplicate(n) {
  2. return [[[n, n]]];
  3. }
  4. _.flatMapDeep([1, 2], duplicate);
  5. // => [1, 1, 2, 2]

_.flatMapDepth(collection, [iteratee=_.identity], [depth=1])

This method is like _.flatMap except that it recursively flattens the mapped results up to depth times.

Since

4.7.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [depth=1] (number): The maximum recursion depth.

Returns

(Array): Returns the new flattened array.

Example

  1. function duplicate(n) {
  2. return [[[n, n]]];
  3. }
  4. _.flatMapDepth([1, 2], duplicate, 2);
  5. // => [[1, 1], [2, 2]]

_.forEach(collection, [iteratee=_.identity])

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

Note: As with other “Collections” methods, objects with a “length” property are iterated like arrays. To avoid this behavior use _.forIn or _.forOwn for object iteration.

Since

0.1.0

Aliases

_.each

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(*): Returns collection.

Example

  1. _.forEach([1, 2], function(value) {
  2. console.log(value);
  3. });
  4. // => Logs `1` then `2`.
  5. _.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  6. console.log(key);
  7. });
  8. // => Logs 'a' then 'b' (iteration order is not guaranteed).

_.forEachRight(collection, [iteratee=_.identity])

This method is like _.forEach except that it iterates over elements of collection from right to left.

Since

2.0.0

Aliases

_.eachRight

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(*): Returns collection.

Example

  1. _.forEachRight([1, 2], function(value) {
  2. console.log(value);
  3. });
  4. // => Logs `2` then `1`.

_.groupBy(collection, [iteratee=_.identity])

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The iteratee to transform keys.

Returns

(Object): Returns the composed aggregate object.

Example

  1. _.groupBy([6.1, 4.2, 6.3], Math.floor);
  2. // => { '4': [4.2], '6': [6.1, 6.3] }
  3. // The `_.property` iteratee shorthand.
  4. _.groupBy(['one', 'two', 'three'], 'length');
  5. // => { '3': ['one', 'two'], '5': ['three'] }

_.includes(collection, value, [fromIndex=0])

Checks if value is in collection. If collection is a string, it’s checked for a substring of value, otherwise SameValueZero is used for equality comparisons. If fromIndex is negative, it’s used as the offset from the end of collection.

Since

0.1.0

Arguments

  1. collection (Array|Object|string): The collection to inspect.
  2. value (*): The value to search for.
  3. [fromIndex=0] (number): The index to search from.

Returns

(boolean): Returns true if value is found, else false.

Example

  1. _.includes([1, 2, 3], 1);
  2. // => true
  3. _.includes([1, 2, 3], 1, 2);
  4. // => false
  5. _.includes({ 'a': 1, 'b': 2 }, 1);
  6. // => true
  7. _.includes('abcd', 'bc');
  8. // => true

_.invokeMap(collection, path, [args])

Invokes the method at path of each element in collection, returning an array of the results of each invoked method. Any additional arguments are provided to each invoked method. If path is a function, it’s invoked for, and this bound to, each element in collection.

Since

4.0.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. path (Array|Function|string): The path of the method to invoke or the function invoked per iteration.
  3. [args] (…*): The arguments to invoke each method with.

Returns

(Array): Returns the array of results.

Example

  1. _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
  2. // => [[1, 5, 7], [1, 2, 3]]
  3. _.invokeMap([123, 456], String.prototype.split, '');
  4. // => [['1', '2', '3'], ['4', '5', '6']]

_.keyBy(collection, [iteratee=_.identity])

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee is invoked with one argument: (value).

Since

4.0.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The iteratee to transform keys.

Returns

(Object): Returns the composed aggregate object.

Example

  1. var array = [
  2. { 'dir': 'left', 'code': 97 },
  3. { 'dir': 'right', 'code': 100 }
  4. ];
  5. _.keyBy(array, function(o) {
  6. return String.fromCharCode(o.code);
  7. });
  8. // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  9. _.keyBy(array, 'dir');
  10. // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }

_.map(collection, [iteratee=_.identity])

Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments:
(value, index|key, collection).

Many lodash methods are guarded to work as iteratees for methods like _.every, _.filter, _.map, _.mapValues, _.reject, and _.some.

The guarded methods are:
ary, chunk, curry, curryRight, drop, dropRight, every, fill, invert, parseInt, random, range, rangeRight, repeat, sampleSize, slice, some, sortBy, split, take, takeRight, template, trim, trimEnd, trimStart, and words

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new mapped array.

Example

  1. function square(n) {
  2. return n * n;
  3. }
  4. _.map([4, 8], square);
  5. // => [16, 64]
  6. _.map({ 'a': 4, 'b': 8 }, square);
  7. // => [16, 64] (iteration order is not guaranteed)
  8. var users = [
  9. { 'user': 'barney' },
  10. { 'user': 'fred' }
  11. ];
  12. // The `_.property` iteratee shorthand.
  13. _.map(users, 'user');
  14. // => ['barney', 'fred']

_.orderBy(collection, [iteratees=[_.identity]], [orders])

This method is like _.sortBy except that it allows specifying the sort orders of the iteratees to sort by. If orders is unspecified, all values are sorted in ascending order. Otherwise, specify an order of “desc” for descending or “asc” for ascending sort order of corresponding values.

Since

4.0.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]): The iteratees to sort by.
  3. [orders] (string[]): The sort orders of iteratees.

Returns

(Array): Returns the new sorted array.

Example

  1. var users = [
  2. { 'user': 'fred', 'age': 48 },
  3. { 'user': 'barney', 'age': 34 },
  4. { 'user': 'fred', 'age': 40 },
  5. { 'user': 'barney', 'age': 36 }
  6. ];
  7. // Sort by `user` in ascending order and by `age` in descending order.
  8. _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
  9. // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]

_.partition(collection, [predicate=_.identity])

Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for. The predicate is invoked with one argument: (value).

Since

3.0.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the array of grouped elements.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': false },
  3. { 'user': 'fred', 'age': 40, 'active': true },
  4. { 'user': 'pebbles', 'age': 1, 'active': false }
  5. ];
  6. _.partition(users, function(o) { return o.active; });
  7. // => objects for [['fred'], ['barney', 'pebbles']]
  8. // The `_.matches` iteratee shorthand.
  9. _.partition(users, { 'age': 1, 'active': false });
  10. // => objects for [['pebbles'], ['barney', 'fred']]
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.partition(users, ['active', false]);
  13. // => objects for [['barney', 'pebbles'], ['fred']]
  14. // The `_.property` iteratee shorthand.
  15. _.partition(users, 'active');
  16. // => objects for [['fred'], ['barney', 'pebbles']]

_.reduce(collection, [iteratee=_.identity], [accumulator])

Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments:
(accumulator, value, index|key, collection).

Many lodash methods are guarded to work as iteratees for methods like _.reduce, _.reduceRight, and _.transform.

The guarded methods are:
assign, defaults, defaultsDeep, includes, merge, orderBy, and sortBy

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [accumulator] (*): The initial value.

Returns

(*): Returns the accumulated value.

Example

  1. _.reduce([1, 2], function(sum, n) {
  2. return sum + n;
  3. }, 0);
  4. // => 3
  5. _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
  6. (result[value] || (result[value] = [])).push(key);
  7. return result;
  8. }, {});
  9. // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)

_.reduceRight(collection, [iteratee=_.identity], [accumulator])

This method is like _.reduce except that it iterates over elements of collection from right to left.

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [accumulator] (*): The initial value.

Returns

(*): Returns the accumulated value.

Example

  1. var array = [[0, 1], [2, 3], [4, 5]];
  2. _.reduceRight(array, function(flattened, other) {
  3. return flattened.concat(other);
  4. }, []);
  5. // => [4, 5, 2, 3, 0, 1]

_.reject(collection, [predicate=_.identity])

The opposite of _.filter; this method returns the elements of collection that predicate does not return truthy for.

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new filtered array.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': false },
  3. { 'user': 'fred', 'age': 40, 'active': true }
  4. ];
  5. _.reject(users, function(o) { return !o.active; });
  6. // => objects for ['fred']
  7. // The `_.matches` iteratee shorthand.
  8. _.reject(users, { 'age': 40, 'active': true });
  9. // => objects for ['barney']
  10. // The `_.matchesProperty` iteratee shorthand.
  11. _.reject(users, ['active', false]);
  12. // => objects for ['fred']
  13. // The `_.property` iteratee shorthand.
  14. _.reject(users, 'active');
  15. // => objects for ['barney']

_.sample(collection)

Gets a random element from collection.

Since

2.0.0

Arguments

  1. collection (Array|Object): The collection to sample.

Returns

(*): Returns the random element.

Example

  1. _.sample([1, 2, 3, 4]);
  2. // => 2

_.sampleSize(collection, [n=1])

Gets n random elements at unique keys from collection up to the size of collection.

Since

4.0.0

Arguments

  1. collection (Array|Object): The collection to sample.
  2. [n=1] (number): The number of elements to sample.

Returns

(Array): Returns the random elements.

Example

  1. _.sampleSize([1, 2, 3], 2);
  2. // => [3, 1]
  3. _.sampleSize([1, 2, 3], 4);
  4. // => [2, 3, 1]

_.shuffle(collection)

Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to shuffle.

Returns

(Array): Returns the new shuffled array.

Example

  1. _.shuffle([1, 2, 3, 4]);
  2. // => [4, 1, 3, 2]

_.size(collection)

Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.

Since

0.1.0

Arguments

  1. collection (Array|Object|string): The collection to inspect.

Returns

(number): Returns the collection size.

Example

  1. _.size([1, 2, 3]);
  2. // => 3
  3. _.size({ 'a': 1, 'b': 2 });
  4. // => 2
  5. _.size('pebbles');
  6. // => 7

_.some(collection, [predicate=_.identity])

Checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy. The predicate is invoked with three arguments: (value, index|key, collection).

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(boolean): Returns true if any element passes the predicate check, else false.

Example

  1. _.some([null, 0, 'yes', false], Boolean);
  2. // => true
  3. var users = [
  4. { 'user': 'barney', 'active': true },
  5. { 'user': 'fred', 'active': false }
  6. ];
  7. // The `_.matches` iteratee shorthand.
  8. _.some(users, { 'user': 'barney', 'active': false });
  9. // => false
  10. // The `_.matchesProperty` iteratee shorthand.
  11. _.some(users, ['active', false]);
  12. // => true
  13. // The `_.property` iteratee shorthand.
  14. _.some(users, 'active');
  15. // => true

_.sortBy(collection, [iteratees=[_.identity]])

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

Since

0.1.0

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratees=[_.identity]] (…(Function|Function[])): The iteratees to sort by.

Returns

(Array): Returns the new sorted array.

Example

  1. var users = [
  2. { 'user': 'fred', 'age': 48 },
  3. { 'user': 'barney', 'age': 36 },
  4. { 'user': 'fred', 'age': 40 },
  5. { 'user': 'barney', 'age': 34 }
  6. ];
  7. _.sortBy(users, [function(o) { return o.user; }]);
  8. // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
  9. _.sortBy(users, ['user', 'age']);
  10. // => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]