These methods operate on JavaScript arrays and array-like values (arguments objects, strings, and objects with a numeric length). Most return a new array; a method that changes its input in place says so explicitly in its description.
_.chunk(array, [size=1])
Available since v3.0.0.
Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to process. |
[size=1] | number | The length of each chunk |
Returns Array — Returns the new array of chunks.
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]_.compact(array)
Available since v0.1.0.
Creates an array with all falsey values removed. The values false, null, 0, -0, 0n, "", undefined, and NaN are falsy.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to compact. |
Returns Array — Returns the new array of filtered values.
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]_.concat(array, [values])
Available since v4.0.0.
Creates a new array concatenating array with any additional arrays and/or values.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to concatenate. |
[values] | ...* | The values to concatenate. |
Returns Array — Returns the new concatenated array.
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
console.log(other);
// => [1, 2, 3, [4]]
console.log(array);
// => [1]_.difference(array, [values])
Available since v0.1.0.
Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.
Note: Unlike _.pullAll, this method returns a new array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[values] | ...Array | The values to exclude. |
Returns Array — Returns the new array of filtered values.
_.difference([2, 1], [2, 3]);
// => [1]_.differenceBy(array, [values], [iteratee=identity])
Available since v4.0.0.
This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument: (value).
Note: Unlike _.pullAllBy, this method returns a new array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[values] | ...Array | The values to exclude. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns the new array of filtered values.
_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2]
// The `_.property` iteratee shorthand.
_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// => [{ 'x': 2 }]_.differenceWith(array, [values], [comparator])
Available since v4.0.0.
This method is like _.difference except that it accepts comparator which is invoked to compare elements of array to values. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arrVal, othVal).
Note: Unlike _.pullAllWith, this method returns a new array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[values] | ...Array | The values to exclude. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns the new array of filtered values.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]_.drop(array, [n=1])
Available since v0.5.0.
Creates a slice of array with n elements dropped from the beginning.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[n=1] | number | The number of elements to drop. |
Returns Array — Returns the slice of array.
_.drop([1, 2, 3]);
// => [2, 3]
_.drop([1, 2, 3], 2);
// => [3]
_.drop([1, 2, 3], 5);
// => []
_.drop([1, 2, 3], 0);
// => [1, 2, 3]_.dropRight(array, [n=1])
Available since v3.0.0.
Creates a slice of array with n elements dropped from the end.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[n=1] | number | The number of elements to drop. |
Returns Array — Returns the slice of array.
_.dropRight([1, 2, 3]);
// => [1, 2]
_.dropRight([1, 2, 3], 2);
// => [1]
_.dropRight([1, 2, 3], 5);
// => []
_.dropRight([1, 2, 3], 0);
// => [1, 2, 3]_.dropRightWhile(array, [predicate=identity])
Available since v3.0.0.
Creates a slice of array excluding elements dropped from the end. Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[predicate=_.identity] | Function | The function invoked per iteration. |
Returns Array — Returns the slice of array.
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']_.dropWhile(array, [predicate=identity])
Available since v3.0.0.
Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[predicate=_.identity] | Function | The function invoked per iteration. |
Returns Array — Returns the slice of array.
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.dropWhile(users, function(o) { return !o.active; });
// => objects for ['pebbles']
// The `_.matches` iteratee shorthand.
_.dropWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['fred', 'pebbles']
// The `_.matchesProperty` iteratee shorthand.
_.dropWhile(users, ['active', false]);
// => objects for ['pebbles']
// The `_.property` iteratee shorthand.
_.dropWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']_.fill(array, value, [start=0], [end=array.length])
Available since v3.2.0.
Fills elements of array with value from start up to, but not including, end.
Note: This method mutates array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to fill. |
value | any | The value to fill array with. |
[start=0] | number | The start position. |
[end=array.length] | number | The end position. |
Returns Array — Returns array.
var array = [1, 2, 3];
_.fill(array, 'a');
console.log(array);
// => ['a', 'a', 'a']
_.fill(Array(3), 2);
// => [2, 2, 2]
_.fill([4, 6, 8, 10], '*', 1, 3);
// => [4, '*', '*', 10]_.findIndex(array, [predicate=identity], [fromIndex=0])
Available since v1.1.0.
This method is like _.find except that it returns the index of the first element predicate returns truthy for instead of the element itself.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[predicate=_.identity] | Function | The function invoked per iteration. |
[fromIndex=0] | number | The index to search from. |
Returns number — Returns the index of the found element, else -1.
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0
// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
// => 2_.findLastIndex(array, [predicate=identity], [fromIndex=array.length-1])
Available since v2.0.0.
This method is like _.findIndex except that it iterates over elements of collection from right to left.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[predicate=_.identity] | Function | The function invoked per iteration. |
[fromIndex=array.length-1] | number | The index to search from. |
Returns number — Returns the index of the found element, else -1.
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
// => 2
// The `_.matches` iteratee shorthand.
_.findLastIndex(users, { 'user': 'barney', 'active': true });
// => 0
// The `_.matchesProperty` iteratee shorthand.
_.findLastIndex(users, ['active', false]);
// => 2
// The `_.property` iteratee shorthand.
_.findLastIndex(users, 'active');
// => 0_.flatten(array)
Available since v0.1.0.
Flattens array a single level deep.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to flatten. |
Returns Array — Returns the new flattened array.
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]_.flattenDeep(array)
Available since v3.0.0.
Recursively flattens array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to flatten. |
Returns Array — Returns the new flattened array.
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]_.flattenDepth(array, [depth=1])
Available since v4.4.0.
Recursively flatten array up to depth times.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to flatten. |
[depth=1] | number | The maximum recursion depth. |
Returns Array — Returns the new flattened array.
var array = [1, [2, [3, [4]], 5]];
_.flattenDepth(array, 1);
// => [1, 2, [3, [4]], 5]
_.flattenDepth(array, 2);
// => [1, 2, 3, [4], 5]_.fromPairs(pairs)
Available since v4.0.0.
The inverse of _.toPairs; this method returns an object composed from key-value pairs.
| Parameter | Type | Description |
|---|---|---|
pairs | Array | The key-value pairs. |
Returns Object — Returns the new object.
_.fromPairs([['a', 1], ['b', 2]]);
// => { 'a': 1, 'b': 2 }_.head(array)
Available since v0.1.0. Alias: _.first.
Gets the first element of array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
Returns any — Returns the first element of array.
_.head([1, 2, 3]);
// => 1
_.head([]);
// => undefined_.indexOf(array, value, [fromIndex=0])
Available since v0.1.0.
Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
value | any | The value to search for. |
[fromIndex=0] | number | The index to search from. |
Returns number — Returns the index of the matched value, else -1.
_.indexOf([1, 2, 1, 2], 2);
// => 1
// Search from the `fromIndex`.
_.indexOf([1, 2, 1, 2], 2, 2);
// => 3_.initial(array)
Available since v0.1.0.
Gets all but the last element of array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
Returns Array — Returns the slice of array.
_.initial([1, 2, 3]);
// => [1, 2]_.intersection([arrays])
Available since v0.1.0.
Creates an array of unique values that are included in all given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
Returns Array — Returns the new array of intersecting values.
_.intersection([2, 1], [2, 3]);
// => [2]_.intersectionBy([arrays], [iteratee=identity])
Available since v4.0.0.
This method is like _.intersection except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns the new array of intersecting values.
_.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [2.1]
// The `_.property` iteratee shorthand.
_.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }]_.intersectionWith([arrays], [comparator])
Available since v4.0.0.
This method is like _.intersection except that it accepts comparator which is invoked to compare elements of arrays. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arrVal, othVal).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns the new array of intersecting values.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.intersectionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }]_.join(array, [separator=','])
Available since v4.0.0.
Converts all elements in array into a string separated by separator.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to convert. |
[separator=','] | string | The element separator. |
Returns string — Returns the joined string.
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'_.last(array)
Available since v0.1.0.
Gets the last element of array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
Returns any — Returns the last element of array.
_.last([1, 2, 3]);
// => 3_.lastIndexOf(array, value, [fromIndex=array.length-1])
Available since v0.1.0.
This method is like _.indexOf except that it iterates over elements of array from right to left.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
value | any | The value to search for. |
[fromIndex=array.length-1] | number | The index to search from. |
Returns number — Returns the index of the matched value, else -1.
_.lastIndexOf([1, 2, 1, 2], 2);
// => 3
// Search from the `fromIndex`.
_.lastIndexOf([1, 2, 1, 2], 2, 2);
// => 1_.nth(array, [n=0])
Available since v4.11.0.
Gets the element at index n of array. If n is negative, the nth element from the end is returned.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[n=0] | number | The index of the element to return. |
Returns any — Returns the nth element of array.
var array = ['a', 'b', 'c', 'd'];
_.nth(array, 1);
// => 'b'
_.nth(array, -2);
// => 'c';_.pull(array, [values])
Available since v2.0.0.
Removes all given values from array using SameValueZero for equality comparisons.
Note: Unlike _.without, this method mutates array. Use remove to remove elements from an array by predicate.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
[values] | ...* | The values to remove. |
Returns Array — Returns array.
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pull(array, 'a', 'c');
console.log(array);
// => ['b', 'b']_.pullAll(array, values)
Available since v4.0.0.
This method is like _.pull except that it accepts an array of values to remove.
Note: Unlike _.difference, this method mutates array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
values | Array | The values to remove. |
Returns Array — Returns array.
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pullAll(array, ['a', 'c']);
console.log(array);
// => ['b', 'b']_.pullAllBy(array, values, [iteratee=identity])
Available since v4.0.0.
This method is like _.pullAll except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The iteratee is invoked with one argument: (value).
Note: Unlike _.differenceBy, this method mutates array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
values | Array | The values to remove. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns array.
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
_.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(array);
// => [{ 'x': 2 }]_.pullAllWith(array, values, [comparator])
Available since v4.6.0.
This method is like _.pullAll except that it accepts comparator which is invoked to compare elements of array to values. The comparator is invoked with two arguments: (arrVal, othVal).
Note: Unlike _.differenceWith, this method mutates array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
values | Array | The values to remove. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns array.
var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
_.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
console.log(array);
// => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]_.pullAt(array, [indexes])
Available since v3.0.0.
Removes elements from array corresponding to indexes and returns an array of removed elements.
Note: Unlike _.at, this method mutates array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
Returns Array — Returns the new array of removed elements.
var array = ['a', 'b', 'c', 'd'];
var pulled = _.pullAt(array, [1, 3]);
console.log(array);
// => ['a', 'c']
console.log(pulled);
// => ['b', 'd']_.remove(array, [predicate=identity])
Available since v2.0.0.
Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. The predicate is invoked with three arguments: (value, index, array).
Note: Unlike _.filter, this method mutates array. Use pull to pull elements from an array by value.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
[predicate=_.identity] | Function | The function invoked per iteration. |
Returns Array — Returns the new array of removed elements.
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
return n % 2 == 0;
});
console.log(array);
// => [1, 3]
console.log(evens);
// => [2, 4]_.reverse(array)
Available since v4.0.0.
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
Note: This method mutates array and is based on Array#reverse.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to modify. |
Returns Array — Returns array.
var array = [1, 2, 3];
_.reverse(array);
// => [3, 2, 1]
console.log(array);
// => [3, 2, 1]_.slice(array, [start=0], [end=array.length])
Available since v3.0.0.
Creates a slice of array from start up to, but not including, end.
Note: This method is used instead of Array#slice to ensure dense arrays are returned.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to slice. |
[start=0] | number | The start position. |
[end=array.length] | number | The end position. |
Returns Array — Returns the slice of array.
_.sortedIndex(array, value)
Available since v0.1.0.
Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.
| Parameter | Type | Description |
|---|---|---|
array | Array | The sorted array to inspect. |
value | any | The value to evaluate. |
Returns number — Returns the index at which value should be inserted into array.
_.sortedIndex([30, 50], 40);
// => 1_.sortedIndexBy(array, value, [iteratee=identity])
Available since v4.0.0.
This method is like _.sortedIndex except that it accepts iteratee which is invoked for value and each element of array to compute their sort ranking. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
array | Array | The sorted array to inspect. |
value | any | The value to evaluate. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns number — Returns the index at which value should be inserted into array.
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 0
// The `_.property` iteratee shorthand.
_.sortedIndexBy(objects, { 'x': 4 }, 'x');
// => 0_.sortedIndexOf(array, value)
Available since v4.0.0.
This method is like _.indexOf except that it performs a binary search on a sorted array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
value | any | The value to search for. |
Returns number — Returns the index of the matched value, else -1.
_.sortedIndexOf([4, 5, 5, 5, 6], 5);
// => 1_.sortedLastIndex(array, value)
Available since v3.0.0.
This method is like _.sortedIndex except that it returns the highest index at which value should be inserted into array in order to maintain its sort order.
| Parameter | Type | Description |
|---|---|---|
array | Array | The sorted array to inspect. |
value | any | The value to evaluate. |
Returns number — Returns the index at which value should be inserted into array.
_.sortedLastIndex([4, 5, 5, 5, 6], 5);
// => 4_.sortedLastIndexBy(array, value, [iteratee=identity])
Available since v4.0.0.
This method is like _.sortedLastIndex except that it accepts iteratee which is invoked for value and each element of array to compute their sort ranking. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
array | Array | The sorted array to inspect. |
value | any | The value to evaluate. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns number — Returns the index at which value should be inserted into array.
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 1
// The `_.property` iteratee shorthand.
_.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
// => 1_.sortedLastIndexOf(array, value)
Available since v4.0.0.
This method is like _.lastIndexOf except that it performs a binary search on a sorted array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
value | any | The value to search for. |
Returns number — Returns the index of the matched value, else -1.
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// => 3_.sortedUniq(array)
Available since v4.0.0.
This method is like _.uniq except that it's designed and optimized for sorted arrays.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
Returns Array — Returns the new duplicate free array.
_.sortedUniq([1, 1, 2]);
// => [1, 2]_.sortedUniqBy(array, [iteratee])
Available since v4.0.0.
This method is like _.uniqBy except that it's designed and optimized for sorted arrays.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[iteratee] | Function | The iteratee invoked per element. |
Returns Array — Returns the new duplicate free array.
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// => [1.1, 2.3]_.tail(array)
Available since v4.0.0.
Gets all but the first element of array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
Returns Array — Returns the slice of array.
_.tail([1, 2, 3]);
// => [2, 3]_.take(array, [n=1])
Available since v0.1.0.
Creates a slice of array with n elements taken from the beginning.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[n=1] | number | The number of elements to take. |
Returns Array — Returns the slice of array.
_.take([1, 2, 3]);
// => [1]
_.take([1, 2, 3], 2);
// => [1, 2]
_.take([1, 2, 3], 5);
// => [1, 2, 3]
_.take([1, 2, 3], 0);
// => []_.takeRight(array, [n=1])
Available since v3.0.0.
Creates a slice of array with n elements taken from the end.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[n=1] | number | The number of elements to take. |
Returns Array — Returns the slice of array.
_.takeRight([1, 2, 3]);
// => [3]
_.takeRight([1, 2, 3], 2);
// => [2, 3]
_.takeRight([1, 2, 3], 5);
// => [1, 2, 3]
_.takeRight([1, 2, 3], 0);
// => []_.takeRightWhile(array, [predicate=identity])
Available since v3.0.0.
Creates a slice of array with elements taken from the end. Elements are taken until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[predicate=_.identity] | Function | The function invoked per iteration. |
Returns Array — Returns the slice of array.
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.takeRightWhile(users, function(o) { return !o.active; });
// => objects for ['fred', 'pebbles']
// The `_.matches` iteratee shorthand.
_.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['pebbles']
// The `_.matchesProperty` iteratee shorthand.
_.takeRightWhile(users, ['active', false]);
// => objects for ['fred', 'pebbles']
// The `_.property` iteratee shorthand.
_.takeRightWhile(users, 'active');
// => []_.takeWhile(array, [predicate=identity])
Available since v3.0.0.
Creates a slice of array with elements taken from the beginning. Elements are taken until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to query. |
[predicate=_.identity] | Function | The function invoked per iteration. |
Returns Array — Returns the slice of array.
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.takeWhile(users, function(o) { return !o.active; });
// => objects for ['barney', 'fred']
// The `_.matches` iteratee shorthand.
_.takeWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.takeWhile(users, ['active', false]);
// => objects for ['barney', 'fred']
// The `_.property` iteratee shorthand.
_.takeWhile(users, 'active');
// => []_.union([arrays])
Available since v0.1.0.
Creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons.
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
Returns Array — Returns the new array of combined values.
_.union([2], [1, 2]);
// => [2, 1]_.unionBy([arrays], [iteratee=identity])
Available since v4.0.0.
This method is like _.union except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed. Result values are chosen from the first array in which the value occurs. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns the new array of combined values.
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]_.unionWith([arrays], [comparator])
Available since v4.0.0.
This method is like _.union except that it accepts comparator which is invoked to compare elements of arrays. Result values are chosen from the first array in which the value occurs. The comparator is invoked with two arguments: (arrVal, othVal).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns the new array of combined values.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.unionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]_.uniq(array)
Available since v0.1.0.
Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
Returns Array — Returns the new duplicate free array.
_.uniq([2, 1, 2]);
// => [2, 1]_.uniqBy(array, [iteratee=identity])
Available since v4.0.0.
This method is like _.uniq except that it accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed. The order of result values is determined by the order they occur in the array. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns the new duplicate free array.
_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]_.uniqWith(array, [comparator])
Available since v4.0.0.
This method is like _.uniq except that it accepts comparator which is invoked to compare elements of array. The order of result values is determined by the order they occur in the array.The comparator is invoked with two arguments: (arrVal, othVal).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns the new duplicate free array.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.uniqWith(objects, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]_.unzip(array)
Available since v1.2.0.
This method is like _.zip except that it accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array of grouped elements to process. |
Returns Array — Returns the new array of regrouped elements.
var zipped = _.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]
_.unzip(zipped);
// => [['a', 'b'], [1, 2], [true, false]]_.unzipWith(array, [iteratee=identity])
Available since v3.8.0.
This method is like _.unzip except that it accepts iteratee to specify how regrouped values should be combined. The iteratee is invoked with the elements of each group: (...group).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array of grouped elements to process. |
[iteratee=_.identity] | Function | The function to combine regrouped values. |
Returns Array — Returns the new array of regrouped elements.
var zipped = _.zip([1, 2], [10, 20], [100, 200]);
// => [[1, 10, 100], [2, 20, 200]]
_.unzipWith(zipped, _.add);
// => [3, 30, 300]_.without(array, [values])
Available since v0.1.0.
Creates an array excluding all given values using SameValueZero for equality comparisons.
Note: Unlike _.pull, this method returns a new array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to inspect. |
[values] | ...* | The values to exclude. |
Returns Array — Returns the new array of filtered values.
_.without([2, 1, 2, 3], 1, 2);
// => [3]_.xor([arrays])
Available since v2.4.0.
Creates an array of unique values that is the symmetric difference of the given arrays. The order of result values is determined by the order they occur in the arrays.
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
Returns Array — Returns the new array of filtered values.
_.xor([2, 1], [2, 3]);
// => [1, 3]_.xorBy([arrays], [iteratee=identity])
Available since v4.0.0.
This method is like _.xor except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which by which they're compared. The order of result values is determined by the order they occur in the arrays. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns Array — Returns the new array of filtered values.
_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2, 3.4]
// The `_.property` iteratee shorthand.
_.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 2 }]_.xorWith([arrays], [comparator])
Available since v4.0.0.
This method is like _.xor except that it accepts comparator which is invoked to compare elements of arrays. The order of result values is determined by the order they occur in the arrays. The comparator is invoked with two arguments: (arrVal, othVal).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to inspect. |
[comparator] | Function | The comparator invoked per element. |
Returns Array — Returns the new array of filtered values.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.xorWith(objects, others, _.isEqual);
// => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]_.zip([arrays])
Available since v0.1.0.
Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to process. |
Returns Array — Returns the new array of grouped elements.
_.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]_.zipObject([props=[]], [values=[]])
Available since v0.4.0.
This method is like _.fromPairs except that it accepts two arrays, one of property identifiers and one of corresponding values.
| Parameter | Type | Description |
|---|---|---|
[props=[]] | Array | The property identifiers. |
[values=[]] | Array | The property values. |
Returns Object — Returns the new object.
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }_.zipObjectDeep([props=[]], [values=[]])
Available since v4.1.0.
This method is like _.zipObject except that it supports property paths.
| Parameter | Type | Description |
|---|---|---|
[props=[]] | Array | The property identifiers. |
[values=[]] | Array | The property values. |
Returns Object — Returns the new object.
_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
// => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }_.zipWith([arrays], [iteratee=identity])
Available since v3.8.0.
This method is like _.zip except that it accepts iteratee to specify how grouped values should be combined. The iteratee is invoked with the elements of each group: (...group).
| Parameter | Type | Description |
|---|---|---|
[arrays] | ...Array | The arrays to process. |
[iteratee=_.identity] | Function | The function to combine grouped values. |
Returns Array — Returns the new array of grouped elements.
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
return a + b + c;
});
// => [111, 222]