Note: All array functions will also work on the arguments object. However, Underscore functions are not designed to work on “sparse” arrays.

first

_.first(array, [n]) Aliases: head, take source

Returns the first element of an array. Passing n will return the first n elements of the array.

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

initial

_.initial(array, [n]) source

Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the last n elements from the result.

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

last

_.last(array, [n]) source

Returns the last element of an array. Passing n will return the last n elements of the array.

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

rest

_.rest(array, [index]) Aliases: tail, drop source

Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.

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

flatten

_.flatten(array, [depth]) source

Flattens a nested array. If you pass true or 1 as the depth, the array will only be flattened a single level. Passing a greater number will cause the flattening to descend deeper into the nesting hierarchy. Omitting the depth argument, or passing false or Infinity, flattens the array all the way to the deepest nesting level.

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

without

_.without(array, *values) source

Returns a copy of the array with all instances of the values removed.

  1. _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  2. => [2, 3, 4]

union

_.union(*arrays) source

Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.

  1. _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  2. => [1, 2, 3, 101, 10]

intersection

_.intersection(*arrays) source

Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

  1. _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  2. => [1, 2]

difference

_.difference(array, *others) source

Similar to without, but returns the values from array that are not present in the other arrays.

  1. _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  2. => [1, 3, 4]

uniq

_.uniq(array, [isSorted], [iteratee]) Alias: unique source

Produces a duplicate-free version of the array, using === to test object equality. In particular only the first occurrence of each value is kept. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iteratee function.

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

zip

_.zip(*arrays) source

Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes.

  1. _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  2. => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]

unzip

_.unzip(array) Alias: transpose source

The opposite of zip. Given an array of arrays, returns a series of new arrays, the first of which contains all of the first elements in the input arrays, the second of which contains all of the second elements, and so on. If you’re working with a matrix of nested arrays, this can be used to transpose the matrix.

  1. _.unzip([["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]);
  2. => [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]

object

_.object(list, [values]) source

Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values. Passing by pairs is the reverse of pairs. If duplicate keys exist, the last value wins.

  1. _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
  2. => {moe: 30, larry: 40, curly: 50}
  3. _.object([['moe', 30], ['larry', 40], ['curly', 50]]);
  4. => {moe: 30, larry: 40, curly: 50}

chunk

_.chunk(array, length) source

Chunks an array into multiple arrays, each containing length or fewer items.

  1. var partners = _.chunk(_.shuffle(kindergarten), 2);
  2. => [["Tyrone", "Elie"], ["Aidan", "Sam"], ["Katrina", "Billie"], ["Little Timmy"]]

indexOf

_.indexOf(array, value, [isSorted]) source

Returns the index at which value can be found in the array, or -1 if value is not present in the array. If you’re working with a large array, and you know that the array is already sorted, pass true for isSorted to use a faster binary search … or, pass a number as the third argument in order to look for the first matching value in the array after the given index. If isSorted is true, this function uses operator < (note).

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

lastIndexOf

_.lastIndexOf(array, value, [fromIndex]) source

Returns the index of the last occurrence of value in the array, or -1 if value is not present. Pass fromIndex to start your search at a given index.

  1. _.lastIndexOf([1, 2, 3, 1, 2, 3\=], 2);
  2. => 4

sortedIndex

_.sortedIndex(array, value, [iteratee], [context]) source

Uses a binary search to determine the smallest index at which the value should be inserted into the array in order to maintain the array‘s sorted order. If an iteratee function is provided, it will be used to compute the sort ranking of each value, including the value you pass. The iteratee may also be the string name of the property to sort by (eg. length). This function uses operator < (note).

  1. _.sortedIndex([10, 20, 30, 40, 50], 35);
  2. => 3
  3. var stooges = [{name: 'moe', age: 40}, {name: 'curly', age: 60}];
  4. _.sortedIndex(stooges, {name: 'larry', age: 50}, 'age');
  5. => 1

findIndex

_.findIndex(array, predicate, [context]) source

Similar to _.indexOf, returns the first index where the predicate truth test passes; otherwise returns -1.

  1. _.findIndex([4, 6, 8, 12], isPrime);
  2. => -1 // not found
  3. _.findIndex([4, 6, 7, 12], isPrime);
  4. => 2

findLastIndex

_.findLastIndex(array, predicate, [context]) source

Like _.findIndex but iterates the array in reverse, returning the index closest to the end where the predicate truth test passes.

  1. var users = [{'id': 1, 'name': 'Bob', 'last': 'Brown'},
  2. {'id': 2, 'name': 'Ted', 'last': 'White'},
  3. {'id': 3, 'name': 'Frank', 'last': 'James'},
  4. {'id': 4, 'name': 'Ted', 'last': 'Jones'}];
  5. _.findLastIndex(users, {
  6. name: 'Ted'
  7. });
  8. => 3

range

_.range([start], stop, [step]) source

A function to create flexibly-numbered lists of integers, handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1 if start is before stop, otherwise -1. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step.

  1. _.range(10);
  2. => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. _.range(1, 11);
  4. => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  5. _.range(0, 30, 5);
  6. => [0, 5, 10, 15, 20, 25]
  7. _.range(0, -10, -1);
  8. => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  9. _.range(0);
  10. => []