_.at(collection [, index1, index2, …])

Creates an array of elements from the specified indexes, or keys, of the collection. Indexes may be specified as individual arguments or as arrays of indexes.

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [index1, index2, ...] (Array|Number|String): The indexes of collection to retrieve, either as individual arguments or arrays.

Returns

(Array): Returns a new array of elements corresponding to the provided indexes.

Example

  1. _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
  2. // => ['a', 'c', 'e']
  3. _.at(['moe', 'larry', 'curly'], 0, 2);
  4. // => ['moe', 'curly']

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

Checks if a given target element is present in a collection using strict equality for comparisons, i.e. ===. If fromIndex is negative, it is used as the offset from the end of the collection.

Aliases

include

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. target (Mixed): The value to check for.
  3. [fromIndex=0] (Number): The index to search from.

Returns

(Boolean): Returns true if the target element is found, else false.

Example

  1. _.contains([1, 2, 3], 1);
  2. // => true
  3. _.contains([1, 2, 3], 1, 2);
  4. // => false
  5. _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
  6. // => true
  7. _.contains('curly', 'ur');
  8. // => true

_.countBy(collection [, callback=identity, thisArg])

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

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns the composed aggregate object.

Example

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

_.every(collection [, callback=identity, thisArg])

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

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Boolean): Returns true if all elements pass the callback check, else false.

Example

  1. _.every([true, 1, null, 'yes'], Boolean);
  2. // => false
  3. var stooges = [
  4. { 'name': 'moe', 'age': 40 },
  5. { 'name': 'larry', 'age': 50 }
  6. ];
  7. // using "_.pluck" callback shorthand
  8. _.every(stooges, 'age');
  9. // => true
  10. // using "_.where" callback shorthand
  11. _.every(stooges, { 'age': 50 });
  12. // => false

_.filter(collection [, callback=identity, thisArg])

Examines each element in a collection, returning an array of all elements the callback returns truthy for. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection).

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array): Returns a new array of elements that passed the callback check.

Example

  1. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  2. // => [2, 4, 6]
  3. var food = [
  4. { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  5. { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  6. ];
  7. // using "_.pluck" callback shorthand
  8. _.filter(food, 'organic');
  9. // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
  10. // using "_.where" callback shorthand
  11. _.filter(food, { 'type': 'fruit' });
  12. // => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]

_.find(collection [, callback=identity, thisArg])

Examines each element in a collection, returning the first that the callback returns truthy for. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection).

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return true for elements that have the properties of the given object, else false.

Aliases

detect, findWhere

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the found element, else undefined.

Example

  1. _.find([1, 2, 3, 4], function(num) {
  2. return num % 2 == 0;
  3. });
  4. // => 2
  5. var food = [
  6. { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  7. { 'name': 'banana', 'organic': true, 'type': 'fruit' },
  8. { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
  9. ];
  10. // using "_.where" callback shorthand
  11. _.find(food, { 'type': 'vegetable' });
  12. // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
  13. // using "_.pluck" callback shorthand
  14. _.find(food, 'organic');
  15. // => { 'name': 'banana', 'organic': true, 'type': 'fruit' }

_.forEach(collection [, callback=identity, thisArg])

Iterates over a collection, executing the callback for each element in the collection. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection). Callbacks may exit iteration early by explicitly returning false.

Aliases

each

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array, Object, String): Returns collection.

Example

  1. _([1, 2, 3]).forEach(alert).join(',');
  2. // => alerts each number and returns '1,2,3'
  3. _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, alert);
  4. // => alerts each number value (order is not guaranteed)

_.groupBy(collection [, callback=identity, thisArg])

Creates an object composed of keys returned from running each element of the collection through the callback. The corresponding value of each key is an array of elements passed to callback that returned the key. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection).

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Object): Returns the composed aggregate object.

Example

  1. _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
  2. // => { '4': [4.2], '6': [6.1, 6.4] }
  3. _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  4. // => { '4': [4.2], '6': [6.1, 6.4] }
  5. // using "_.pluck" callback shorthand
  6. _.groupBy(['one', 'two', 'three'], 'length');
  7. // => { '3': ['one', 'two'], '5': ['three'] }

_.invoke(collection, methodName [, arg1, arg2, …])

Invokes the method named by methodName on each element in the collection, returning an array of the results of each invoked method. Additional arguments will be passed to each invoked method. If methodName is a function, it will be invoked for, and this bound to, each element in the collection.

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. methodName (Function|String): The name of the method to invoke or the function invoked per iteration.
  3. [arg1, arg2, ...] (Mixed): Arguments to invoke the method with.

Returns

(Array): Returns a new array of the results of each invoked method.

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

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

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return true for elements that have the properties of the given object, else false.

Aliases

collect

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array): Returns a new array of the results of each callback execution.

Example

  1. _.map([1, 2, 3], function(num) { return num * 3; });
  2. // => [3, 6, 9]
  3. _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
  4. // => [3, 6, 9] (order is not guaranteed)
  5. var stooges = [
  6. { 'name': 'moe', 'age': 40 },
  7. { 'name': 'larry', 'age': 50 }
  8. ];
  9. // using "_.pluck" callback shorthand
  10. _.map(stooges, 'name');
  11. // => ['moe', 'larry']

_.max(collection [, callback=identity, thisArg])

Retrieves the maximum value of an array. If callback is passed, it will be executed for each value in the array to generate the criterion by which the value is ranked. The callback is bound to thisArg and invoked with three arguments; (value, index, collection).

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the maximum value.

Example

  1. _.max([4, 2, 8, 6]);
  2. // => 8
  3. var stooges = [
  4. { 'name': 'moe', 'age': 40 },
  5. { 'name': 'larry', 'age': 50 }
  6. ];
  7. _.max(stooges, function(stooge) { return stooge.age; });
  8. // => { 'name': 'larry', 'age': 50 };
  9. // using "_.pluck" callback shorthand
  10. _.max(stooges, 'age');
  11. // => { 'name': 'larry', 'age': 50 };

_.min(collection [, callback=identity, thisArg])

Retrieves the minimum value of an array. If callback is passed, it will be executed for each value in the array to generate the criterion by which the value is ranked. The callback is bound to thisArg and invoked with three arguments; (value, index, collection).

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the minimum value.

Example

  1. _.min([4, 2, 8, 6]);
  2. // => 2
  3. var stooges = [
  4. { 'name': 'moe', 'age': 40 },
  5. { 'name': 'larry', 'age': 50 }
  6. ];
  7. _.min(stooges, function(stooge) { return stooge.age; });
  8. // => { 'name': 'moe', 'age': 40 };
  9. // using "_.pluck" callback shorthand
  10. _.min(stooges, 'age');
  11. // => { 'name': 'moe', 'age': 40 };

_.pluck(collection, property)

Retrieves the value of a specified property from all elements in the collection.

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. property (String): The property to pluck.

Returns

(Array): Returns a new array of property values.

Example

  1. var stooges = [
  2. { 'name': 'moe', 'age': 40 },
  3. { 'name': 'larry', 'age': 50 }
  4. ];
  5. _.pluck(stooges, 'name');
  6. // => ['moe', 'larry']

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

Reduces a collection to a value which is the accumulated result of running each element in the collection through the callback, where each successive callback execution consumes the return value of the previous execution. If accumulator is not passed, the first element of the collection will be used as the initial accumulator value. The callback is bound to thisArg and invoked with four arguments; (accumulator, value, index|key, collection).

Aliases

foldl, inject

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [accumulator] (Mixed): Initial value of the accumulator.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the accumulated value.

Example

  1. var sum = _.reduce([1, 2, 3], function(sum, num) {
  2. return sum + num;
  3. });
  4. // => 6
  5. var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  6. result[key] = num * 3;
  7. return result;
  8. }, {});
  9. // => { 'a': 3, 'b': 6, 'c': 9 }

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

This method is similar to _.reduce, except that it iterates over a collection from right to left.

Aliases

foldr

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. [callback=identity] (Function): The function called per iteration.
  3. [accumulator] (Mixed): Initial value of the accumulator.
  4. [thisArg] (Mixed): The this binding of callback.

Returns

(Mixed): Returns the accumulated value.

Example

  1. var list = [[0, 1], [2, 3], [4, 5]];
  2. var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  3. // => [4, 5, 2, 3, 0, 1]

_.reject(collection [, callback=identity, thisArg])

The opposite of _.filter, this method returns the elements of a collection that callback does not return truthy for.

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array): Returns a new array of elements that did not pass the callback check.

Example

  1. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  2. // => [1, 3, 5]
  3. var food = [
  4. { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  5. { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  6. ];
  7. // using "_.pluck" callback shorthand
  8. _.reject(food, 'organic');
  9. // => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]
  10. // using "_.where" callback shorthand
  11. _.reject(food, { 'type': 'fruit' });
  12. // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]

_.shuffle(collection)

Creates an array of shuffled array values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.

Arguments

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

Returns

(Array): Returns a new shuffled collection.

Example

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

_.size(collection)

Gets the size of the collection by returning collection.length for arrays and array-like objects or the number of own enumerable properties for objects.

Arguments

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

Returns

(Number): Returns collection.length or number of own enumerable properties.

Example

  1. _.size([1, 2]);
  2. // => 2
  3. _.size({ 'one': 1, 'two': 2, 'three': 3 });
  4. // => 3
  5. _.size('curly');
  6. // => 5

_.some(collection [, callback=identity, thisArg])

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

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Boolean): Returns true if any element passes the callback check, else false.

Example

  1. _.some([null, 0, 'yes', false], Boolean);
  2. // => true
  3. var food = [
  4. { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  5. { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  6. ];
  7. // using "_.pluck" callback shorthand
  8. _.some(food, 'organic');
  9. // => true
  10. // using "_.where" callback shorthand
  11. _.some(food, { 'type': 'meat' });
  12. // => false

_.sortBy(collection [, callback=identity, thisArg])

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

If a property name is passed for callback, the created “_.pluck” style callback will return the property value of the given element.

If an object is passed for callback, the created “_.where” style callback will return 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. [callback=identity] (Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a “.pluck” or “.where” style callback, respectively.
  3. [thisArg] (Mixed): The this binding of callback.

Returns

(Array): Returns a new array of sorted elements.

Example

  1. _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
  2. // => [3, 1, 2]
  3. _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
  4. // => [3, 1, 2]
  5. // using "_.pluck" callback shorthand
  6. _.sortBy(['banana', 'strawberry', 'apple'], 'length');
  7. // => ['apple', 'banana', 'strawberry']

_.toArray(collection)

Converts the collection to an array.

Arguments

  1. collection (Array|Object|String): The collection to convert.

Returns

(Array): Returns the new converted array.

Example

  1. (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  2. // => [2, 3, 4]

_.where(collection, properties)

Examines each element in a collection, returning an array of all elements that have the given properties. When checking properties, this method performs a deep comparison between values to determine if they are equivalent to each other.

Arguments

  1. collection (Array|Object|String): The collection to iterate over.
  2. properties (Object): The object of property values to filter by.

Returns

(Array): Returns a new array of elements that have the given properties.

Example

  1. var stooges = [
  2. { 'name': 'moe', 'age': 40 },
  3. { 'name': 'larry', 'age': 50 }
  4. ];
  5. _.where(stooges, { 'age': 40 });
  6. // => [{ 'name': 'moe', 'age': 40 }]