each

_.each(list, iteratee, [context]) Alias: forEach source

Iterates over a list of elements, yielding each in turn to an iteratee function. The iteratee is bound to the context object, if one is passed. Each invocation of iteratee is called with three arguments: (element, index, list). If list is a JavaScript object, iteratee‘s arguments will be (value, key, list). Returns the list for chaining.

  1. _.each([1, 2, 3], alert);
  2. => alerts each number in turn...
  3. _.each({one: 1, two: 2, three: 3}, alert);
  4. => alerts each number value in turn...

Note: Collection functions work on arrays, objects, and array-like objects such as arguments, NodeList and similar. But it works by duck-typing, so avoid passing objects with a numeric length property. It’s also good to note that an each loop cannot be broken out of — to break, use **\.find** instead._

map

_.map(list, iteratee, [context]) Alias: collect source

Produces a new array of values by mapping each value in list through a transformation function (iteratee). The iteratee is passed three arguments: the value, then the index (or key) of the iteration, and finally a reference to the entire list.

  1. _.map([1, 2, 3], function(num){ return num * 3; });
  2. => [3, 6, 9]
  3. _.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
  4. => [3, 6, 9]
  5. _.map([[1, 2], [3, 4]], _.first);
  6. => [1, 3]

reduce

_.reduce(list, iteratee, [memo], [context]) Aliases: inject, foldl source

Also known as inject and foldl, reduce boils down a list of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iteratee. The iteratee is passed four arguments: the memo, then the value and index (or key) of the iteration, and finally a reference to the entire list.

If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element of the list. The first element is instead passed as the memo in the invocation of the iteratee on the next element in the list.

  1. var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
  2. => 6

reduceRight

_.reduceRight(list, iteratee, [memo], [context]) Alias: foldr source

The right-associative version of reduce. Foldr is not as useful in JavaScript as it would be in a language with lazy evaluation.

  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]

find

_.find(list, predicate, [context]) Alias: detect source

Looks through each value in the list, returning the first one that passes a truth test (predicate), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn’t traverse the entire list. predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  2. => 2

filter

_.filter(list, predicate, [context]) Alias: select source

Looks through each value in the list, returning an array of all the values that pass a truth test (predicate). predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  2. => [2, 4, 6]

findWhere

_.findWhere(list, properties) source

Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.

If no match is found, or if list is empty, undefined will be returned.

  1. _.findWhere(publicServicePulitzers, {newsroom: "The New York Times"});
  2. => {year: 1918, newsroom: "The New York Times",
  3. reason: "For its public service in publishing in full so many official reports,
  4. documents and speeches by European statesmen relating to the progress and
  5. conduct of the war."}

where

_.where(list, properties) source

Looks through each value in the list, returning an array of all the values that matches the key-value pairs listed in properties.

  1. _.where(listOfPlays, {author: "Shakespeare", year: 1611});
  2. => [{title: "Cymbeline", author: "Shakespeare", year: 1611},
  3. {title: "The Tempest", author: "Shakespeare", year: 1611}]

reject

_.reject(list, predicate, [context]) source

Returns the values in list without the elements that the truth test (predicate) passes. The opposite of filter. predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
  2. => [1, 3, 5]

every

_.every(list, [predicate], [context]) Alias: all source

Returns true if all of the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a false element is found. predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. _.every([2, 4, 5], function(num) { return num % 2 == 0; });
  2. => false

some

_.some(list, [predicate], [context]) Alias: any source

Returns true if any of the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a true element is found. predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. _.some([null, 0, 'yes', false]);
  2. => true

contains

_.contains(list, value, [fromIndex]) Aliases: include, includes source

Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.

  1. _.contains([1, 2, 3], 3);
  2. => true

invoke

_.invoke(list, methodName, *arguments) source

Calls the method named by methodName on each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.

  1. _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  2. => [[1, 5, 7], [1, 2, 3]]

pluck

_.pluck(list, propertyName) source

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

  1. var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
  2. _.pluck(stooges, 'name');
  3. => ["moe", "larry", "curly"]

max

_.max(list, [iteratee], [context]) source

Returns the maximum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. -Infinity is returned if list is empty, so an isEmpty guard may be required. This function can currently only compare numbers reliably. This function uses operator < (note).

  1. var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
  2. _.max(stooges, function(stooge){ return stooge.age; });
  3. => {name: 'curly', age: 60};

min

_.min(list, [iteratee], [context]) source

Returns the minimum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. Infinity is returned if list is empty, so an isEmpty guard may be required. This function can currently only compare numbers reliably. This function uses operator < (note).

  1. var numbers = [10, 5, 100, 2, 1000];
  2. _.min(numbers);
  3. => 2

sortBy

_.sortBy(list, iteratee, [context]) source

Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length). This function uses operator < (note).

  1. _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
  2. => [5, 4, 6, 3, 1, 2]
  3. var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
  4. _.sortBy(stooges, 'name');
  5. => [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];

groupBy

_.groupBy(list, iteratee, [context]) source

Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.

  1. _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
  2. => {1: [1.3], 2: [2.1, 2.4]}
  3. _.groupBy(['one', 'two', 'three'], 'length');
  4. => {3: ["one", "two"], 5: ["three"]}

indexBy

_.indexBy(list, iteratee, [context]) source

Given a list, and an iteratee function that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.

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

countBy

_.countBy(list, iteratee, [context]) source

Sorts a list into groups and returns a count for the number of objects in each group. Similar to groupBy, but instead of returning a list of values, returns a count for the number of values in that group.

  1. _.countBy([1, 2, 3, 4, 5], function(num) {
  2. return num % 2 == 0 ? 'even': 'odd';
  3. });
  4. => {odd: 3, even: 2}

shuffle

_.shuffle(list) source

Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle.

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

sample

_.sample(list, [n]) source

Produce a random sample from the list. Pass a number to return n random elements from the list. Otherwise a single random item will be returned.

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

toArray

_.toArray(list) source

Creates a real Array from the list (anything that can be iterated over). Useful for transmuting the arguments object.

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

size

_.size(list) source

Return the number of values in the list.

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

partition

_.partition(list, predicate) source

Split list into two arrays: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate. predicate is transformed through iteratee to facilitate shorthand syntaxes.

  1. _.partition([0, 1, 2, 3, 4, 5], isOdd);
  2. => [[1, 3, 5], [0, 2, 4]]

compact

_.compact(list) source

Returns a copy of the list with all falsy values removed. In JavaScript, false, null, 0, “”, undefined and NaN are all falsy.

  1. _.compact([0, 1, false, 2, '', 3]);
  2. => [1, 2, 3]