It has been 1485 days since the last update, the content of the article may be outdated.
Array Functions
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 Returns the first element of an array. Passing n will return the first n elements of the array.
plaintext
_.first([5, 4, 3, 2, 1]); => 5
initial
_.initial(array, [n]) 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.
plaintext
_.initial([5, 4, 3, 2, 1]); => [5, 4, 3, 2]
last
_.last(array, [n]) Returns the last element of an array. Passing n will return the last n elements of the array.
plaintext
_.last([5, 4, 3, 2, 1]); => 1
rest
_.rest(array, [index]) Aliases: tail, drop Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.
plaintext
_.rest([5, 4, 3, 2, 1]); => [4, 3, 2, 1]
compact
_.compact(array) Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, “”, undefined and NaN are all falsy.
plaintext
_.compact([0, 1, false, 2, '', 3]); => [1, 2, 3]
flatten
_.flatten(array, [shallow]) Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.
Produces a duplicate-free version of the array, using === to test object equality. In particular only the first occurence 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.
plaintext
_.uniq([1, 2, 1, 4, 1, 3]); => [1, 2, 4, 3]
zip
plaintext
_.zip(*arrays)
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. Use with apply to pass in an array of arrays. If you’re working with a matrix of nested arrays, this can be used to transpose the matrix.
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.
Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values. If duplicate keys exist, the last value wins.
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.
plaintext
_.indexOf([1, 2, 3], 2); => 1
lastIndexOf
plaintext
_.lastIndexOf(array, value, [fromIndex])
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.
plaintext
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2); => 4
sortedIndex
plaintext
_.sortedIndex(list, value, [iteratee], [context])
Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list’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).
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. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step, exclusive. Note that ranges that stop before they start are considered to be zero-length instead of negative — if you’d like a negative range, use a negative step.