Arrays Flashcards
_.first(array, [n])
Returns the first element of an array. Passing n will return the first n elements of the array.
_.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.
_.last(array, [n])
Returns the last element of an array. Passing n will return the last n elements of the array.
_.rest(array, [index])
Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.
_.compact(array)
Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, “”, undefined and NaN are all falsy.
_.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.
_.without(array, *values)
Returns a copy of the array with all instances of the values removed.
_.union(*arrays)
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.
_.intersection(*arrays)
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.
_.difference(array, *others)
Returns the values from array that are not present in the other arrays.
_.uniq(array, [isSorted], [iteratee])
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.
_.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.
_.unzip(*arrays)
Given a number 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.
_.object(list, [values])
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.
_.indexOf(array, value, [isSorted])
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
_.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.