_.at(collection, [props])

Creates an array of elements corresponding to the given keys, or indexes, of collection. Keys may be specified as individual arguments or as arrays of keys.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [props] (…(number|number[]|string|string[]): The property names or indexes of elements to pick, specified individually or in arrays.

Returns

(Array): Returns the new array of picked elements.

Example

  1. _.at(['a', 'b', 'c'], [0, 2]);
  2. // => ['a', 'c']
  3. _.at(['barney', 'fred', 'pebbles'], 0, 2);
  4. // => ['barney', 'pebbles']

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

Creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the number of times the key was returned by iteratee. The iteratee is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns the composed aggregate object.

Example

  1. _.countBy([4.3, 6.1, 6.4], function(n) {
  2. return Math.floor(n);
  3. });
  4. // => { '4': 1, '6': 2 }
  5. _.countBy([4.3, 6.1, 6.4], function(n) {
  6. return this.floor(n);
  7. }, Math);
  8. // => { '4': 1, '6': 2 }
  9. _.countBy(['one', 'two', 'three'], 'length');
  10. // => { '3': 2, '5': 1 }

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

Checks if predicate returns truthy for all elements of collection. The predicate is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Aliases

_.all

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

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', 'active': false },
  5. { 'user': 'fred', 'active': false }
  6. ];
  7. // using the `_.matches` callback shorthand
  8. _.every(users, { 'user': 'barney', 'active': false });
  9. // => false
  10. // using the `_.matchesProperty` callback shorthand
  11. _.every(users, 'active', false);
  12. // => true
  13. // using the `_.property` callback shorthand
  14. _.every(users, 'active');
  15. // => false

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

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

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Aliases

_.select

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

(Array): Returns the new filtered array.

Example

  1. _.filter([4, 5, 6], function(n) {
  2. return n % 2 == 0;
  3. });
  4. // => [4, 6]
  5. var users = [
  6. { 'user': 'barney', 'age': 36, 'active': true },
  7. { 'user': 'fred', 'age': 40, 'active': false }
  8. ];
  9. // using the `_.matches` callback shorthand
  10. _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
  11. // => ['barney']
  12. // using the `_.matchesProperty` callback shorthand
  13. _.pluck(_.filter(users, 'active', false), 'user');
  14. // => ['fred']
  15. // using the `_.property` callback shorthand
  16. _.pluck(_.filter(users, 'active'), 'user');
  17. // => ['barney']

.find(collection, [predicate=.identity], [thisArg])

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

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Aliases

_.detect

Arguments

  1. collection (Array|Object|string): The collection to search.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

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. _.result(_.find(users, function(chr) {
  7. return chr.age < 40;
  8. }), 'user');
  9. // => 'barney'
  10. // using the `_.matches` callback shorthand
  11. _.result(_.find(users, { 'age': 1, 'active': true }), 'user');
  12. // => 'pebbles'
  13. // using the `_.matchesProperty` callback shorthand
  14. _.result(_.find(users, 'active', false), 'user');
  15. // => 'fred'
  16. // using the `_.property` callback shorthand
  17. _.result(_.find(users, 'active'), 'user');
  18. // => 'barney'

.findLast(collection, [predicate=.identity], [thisArg])

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

Arguments

  1. collection (Array|Object|string): The collection to search.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

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

Example

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

_.findWhere(collection, source)

Performs a deep comparison between each element in collection and the source object, returning the first element that has equivalent property values.

Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and strings. Objects are compared by their own, not inherited, enumerable properties. For comparing a single own or inherited property value see _.matchesProperty.

Arguments

  1. collection (Array|Object|string): The collection to search.
  2. source (Object): The object of property values to match.

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. ];
  5. _.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');
  6. // => 'barney'
  7. _.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');
  8. // => 'fred'

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

Iterates over elements of collection invoking iteratee for each element. The iteratee is bound to thisArg and 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 _.forIn or _.forOwn may be used for object iteration.

Aliases

_.each

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Array|Object|string): Returns collection.

Example

  1. _([1, 2]).forEach(function(n) {
  2. console.log(n);
  3. }).value();
  4. // => logs each value from left to right and returns the array
  5. _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
  6. console.log(n, key);
  7. });
  8. // => logs each value-key pair and returns the object (iteration order is not guaranteed)

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

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

Aliases

_.eachRight

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Array|Object|string): Returns collection.

Example

  1. _([1, 2]).forEachRight(function(n) {
  2. console.log(n);
  3. }).value();
  4. // => logs each value from right to left and returns the array

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

Creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is an array of the elements responsible for generating the key. The iteratee is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns the composed aggregate object.

Example

  1. _.groupBy([4.2, 6.1, 6.4], function(n) {
  2. return Math.floor(n);
  3. });
  4. // => { '4': [4.2], '6': [6.1, 6.4] }
  5. _.groupBy([4.2, 6.1, 6.4], function(n) {
  6. return this.floor(n);
  7. }, Math);
  8. // => { '4': [4.2], '6': [6.1, 6.4] }
  9. // using the `_.property` callback shorthand
  10. _.groupBy(['one', 'two', 'three'], 'length');
  11. // => { '3': ['one', 'two'], '5': ['three'] }

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

Checks if target is in collection using SameValueZero for equality comparisons. If fromIndex is negative, it’s used as the offset from the end of collection.

Aliases

.contains, .include

Arguments

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

Returns

(boolean): Returns true if a matching element is found, else false.

Example

  1. _.includes([1, 2, 3], 1);
  2. // => true
  3. _.includes([1, 2, 3], 1, 2);
  4. // => false
  5. _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
  6. // => true
  7. _.includes('pebbles', 'eb');
  8. // => true

.indexBy(collection, [iteratee=.identity], [thisArg])

Creates an object composed of keys generated from the results of running each element of collection through iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee function is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Object): Returns the composed aggregate object.

Example

  1. var keyData = [
  2. { 'dir': 'left', 'code': 97 },
  3. { 'dir': 'right', 'code': 100 }
  4. ];
  5. _.indexBy(keyData, 'dir');
  6. // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  7. _.indexBy(keyData, function(object) {
  8. return String.fromCharCode(object.code);
  9. });
  10. // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  11. _.indexBy(keyData, function(object) {
  12. return this.fromCharCode(object.code);
  13. }, String);
  14. // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }

_.invoke(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 methodName is a function it’s invoked for, and this bound to, each element in collection.

Arguments

  1. collection (Array|Object|string): 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 the method with.

Returns

(Array): Returns the array of results.

Example

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

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

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

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

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

The guarded methods are:
ary, callback, chunk, clone, create, curry, curryRight, drop, dropRight, every, fill, flatten, invert, max, min, parseInt, slice, sortBy, take, takeRight, template, trim, trimLeft, trimRight, trunc, random, range, sample, some, sum, uniq, and words

Aliases

_.collect

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Array): Returns the new mapped array.

Example

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

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

Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, while the second of which contains elements predicate returns falsey for. The predicate is bound to thisArg and invoked with three arguments: (value, index|key, collection).

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

(Array): Returns the array of grouped elements.

Example

  1. _.partition([1, 2, 3], function(n) {
  2. return n % 2;
  3. });
  4. // => [[1, 3], [2]]
  5. _.partition([1.2, 2.3, 3.4], function(n) {
  6. return this.floor(n) % 2;
  7. }, Math);
  8. // => [[1.2, 3.4], [2.3]]
  9. var users = [
  10. { 'user': 'barney', 'age': 36, 'active': false },
  11. { 'user': 'fred', 'age': 40, 'active': true },
  12. { 'user': 'pebbles', 'age': 1, 'active': false }
  13. ];
  14. var mapper = function(array) {
  15. return _.pluck(array, 'user');
  16. };
  17. // using the `_.matches` callback shorthand
  18. _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);
  19. // => [['pebbles'], ['barney', 'fred']]
  20. // using the `_.matchesProperty` callback shorthand
  21. _.map(_.partition(users, 'active', false), mapper);
  22. // => [['barney', 'pebbles'], ['fred']]
  23. // using the `_.property` callback shorthand
  24. _.map(_.partition(users, 'active'), mapper);
  25. // => [['fred'], ['barney', 'pebbles']]

_.pluck(collection, path)

Gets the property value of path from all elements in collection.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. path (Array|string): The path of the property to pluck.

Returns

(Array): Returns the property values.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36 },
  3. { 'user': 'fred', 'age': 40 }
  4. ];
  5. _.pluck(users, 'user');
  6. // => ['barney', 'fred']
  7. var userIndex = _.indexBy(users, 'user');
  8. _.pluck(userIndex, 'age');
  9. // => [36, 40] (iteration order is not guaranteed)

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

Reduces collection to a value which is the accumulated result of running each element in collection through iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not provided the first element of collection is used as the initial value. The iteratee is bound to thisArg and 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, sortByAll, and sortByOrder

Aliases

.foldl, .inject

Arguments

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

Returns

(*): Returns the accumulated value.

Example

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

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

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

Aliases

_.foldr

Arguments

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

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], [thisArg])

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

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

Returns

(Array): Returns the new filtered array.

Example

  1. _.reject([1, 2, 3, 4], function(n) {
  2. return n % 2 == 0;
  3. });
  4. // => [1, 3]
  5. var users = [
  6. { 'user': 'barney', 'age': 36, 'active': false },
  7. { 'user': 'fred', 'age': 40, 'active': true }
  8. ];
  9. // using the `_.matches` callback shorthand
  10. _.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');
  11. // => ['barney']
  12. // using the `_.matchesProperty` callback shorthand
  13. _.pluck(_.reject(users, 'active', false), 'user');
  14. // => ['fred']
  15. // using the `_.property` callback shorthand
  16. _.pluck(_.reject(users, 'active'), 'user');
  17. // => ['barney']

_.sample(collection, [n])

Gets a random element or n random elements from a collection.

Arguments

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

Returns

(*): Returns the random sample(s).

Example

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

_.shuffle(collection)

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

Arguments

  1. collection (Array|Object|string): 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 properties for objects.

Arguments

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

Returns

(number): Returns the size of collection.

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], [thisArg])

Checks if predicate returns truthy for any element of collection. The function returns as soon as it finds a passing value and does not iterate over the entire collection. The predicate is bound to thisArg and invoked with three arguments: (value, index|key, collection).

If a property name is provided for predicate the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for predicate the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Aliases

_.any

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [predicate=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of predicate.

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. // using the `_.matches` callback shorthand
  8. _.some(users, { 'user': 'barney', 'active': false });
  9. // => false
  10. // using the `_.matchesProperty` callback shorthand
  11. _.some(users, 'active', false);
  12. // => true
  13. // using the `_.property` callback shorthand
  14. _.some(users, 'active');
  15. // => true

.sortBy(collection, [iteratee=.identity], [thisArg])

Creates an array of elements, sorted in ascending order by the results of running each element in a collection through iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratee is bound to thisArg and invoked with three arguments:
(value, index|key, collection).

If a property name is provided for iteratee the created _.property style callback returns the property value of the given element.

If a value is also provided for thisArg the created _.matchesProperty style callback returns true for elements that have a matching property value, else false.

If an object is provided for iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. [iteratee=_.identity] (Function|Object|string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

Returns

(Array): Returns the new sorted array.

Example

  1. _.sortBy([1, 2, 3], function(n) {
  2. return Math.sin(n);
  3. });
  4. // => [3, 1, 2]
  5. _.sortBy([1, 2, 3], function(n) {
  6. return this.sin(n);
  7. }, Math);
  8. // => [3, 1, 2]
  9. var users = [
  10. { 'user': 'fred' },
  11. { 'user': 'pebbles' },
  12. { 'user': 'barney' }
  13. ];
  14. // using the `_.property` callback shorthand
  15. _.pluck(_.sortBy(users, 'user'), 'user');
  16. // => ['barney', 'fred', 'pebbles']

_.sortByAll(collection, iteratees)

This method is like _.sortBy except that it can sort by multiple iteratees or property names.

If a property name is provided for an iteratee the created _.property style callback returns the property value of the given element.

If an object is provided for an iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. iteratees (…(Function|Function[]|Object|Object[]|string|string[]): The iteratees to sort by, specified as individual values or arrays of values.

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': 42 },
  5. { 'user': 'barney', 'age': 34 }
  6. ];
  7. _.map(_.sortByAll(users, ['user', 'age']), _.values);
  8. // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
  9. _.map(_.sortByAll(users, 'user', function(chr) {
  10. return Math.floor(chr.age / 10);
  11. }), _.values);
  12. // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]

_.sortByOrder(collection, iteratees, [orders])

This method is like _.sortByAll 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, a value is sorted in ascending order if its corresponding order is “asc”, and descending if “desc”.

If a property name is provided for an iteratee the created _.property style callback returns the property value of the given element.

If an object is provided for an iteratee the created _.matches style callback returns true for elements that have the properties of the given object, else false.

Arguments

  1. collection (Array|Object|string): The collection to iterate over.
  2. iteratees (Function[]|Object[]|string[]): The iteratees to sort by.
  3. [orders] (boolean[]): 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': 42 },
  5. { 'user': 'barney', 'age': 36 }
  6. ];
  7. // sort by `user` in ascending order and by `age` in descending order
  8. _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values);
  9. // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]

_.where(collection, source)

Performs a deep comparison between each element in collection and the source object, returning an array of all elements that have equivalent property values.

Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and strings. Objects are compared by their own, not inherited, enumerable properties. For comparing a single own or inherited property value see _.matchesProperty.

Arguments

  1. collection (Array|Object|string): The collection to search.
  2. source (Object): The object of property values to match.

Returns

(Array): Returns the new filtered array.

Example

  1. var users = [
  2. { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },
  3. { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }
  4. ];
  5. _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');
  6. // => ['barney']
  7. _.pluck(_.where(users, { 'pets': ['dino'] }), 'user');
  8. // => ['fred']