_.chunk(array, [size=1])

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.

Since

3.0.0

Arguments

  1. array (Array): The array to process.
  2. [size=1] (number): The length of each chunk

Returns

(Array): Returns the new array of chunks.

Example

  1. _.chunk(['a', 'b', 'c', 'd'], 2);
  2. // => [['a', 'b'], ['c', 'd']]
  3. _.chunk(['a', 'b', 'c', 'd'], 3);
  4. // => [['a', 'b', 'c'], ['d']]

_.compact(array)

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

Since

0.1.0

Arguments

  1. array (Array): The array to compact.

Returns

(Array): Returns the new array of filtered values.

Example

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

_.concat(array, [values])

Creates a new array concatenating array with any additional arrays and/or values.

Since

4.0.0

Arguments

  1. array (Array): The array to concatenate.
  2. [values] (…*): The values to concatenate.

Returns

(Array): Returns the new concatenated array.

Example

  1. var array = [1];
  2. var other = _.concat(array, 2, [3], [[4]]);
  3. console.log(other);
  4. // => [1, 2, 3, [4]]
  5. console.log(array);
  6. // => [1]

_.difference(array, [values])

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.

Since

0.1.0

Arguments

  1. array (Array): The array to inspect.
  2. [values] (…Array): The values to exclude.

Returns

(Array): Returns the new array of filtered values.

Example

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

_.differenceBy(array, [values], [iteratee=_.identity])

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.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [values] (…Array): The values to exclude.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new array of filtered values.

Example

  1. _.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  2. // => [1.2]
  3. // The `_.property` iteratee shorthand.
  4. _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
  5. // => [{ 'x': 2 }]

_.differenceWith(array, [values], [comparator])

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.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [values] (…Array): The values to exclude.
  3. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns the new array of filtered values.

Example

  1. var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  2. _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
  3. // => [{ 'x': 2, 'y': 1 }]

_.drop(array, [n=1])

Creates a slice of array with n elements dropped from the beginning.

Since

0.5.0

Arguments

  1. array (Array): The array to query.
  2. [n=1] (number): The number of elements to drop.

Returns

(Array): Returns the slice of array.

Example

  1. _.drop([1, 2, 3]);
  2. // => [2, 3]
  3. _.drop([1, 2, 3], 2);
  4. // => [3]
  5. _.drop([1, 2, 3], 5);
  6. // => []
  7. _.drop([1, 2, 3], 0);
  8. // => [1, 2, 3]

_.dropRight(array, [n=1])

Creates a slice of array with n elements dropped from the end.

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [n=1] (number): The number of elements to drop.

Returns

(Array): Returns the slice of array.

Example

  1. _.dropRight([1, 2, 3]);
  2. // => [1, 2]
  3. _.dropRight([1, 2, 3], 2);
  4. // => [1]
  5. _.dropRight([1, 2, 3], 5);
  6. // => []
  7. _.dropRight([1, 2, 3], 0);
  8. // => [1, 2, 3]

_.dropRightWhile(array, [predicate=_.identity])

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).

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the slice of array.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': true },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': false }
  5. ];
  6. _.dropRightWhile(users, function(o) { return !o.active; });
  7. // => objects for ['barney']
  8. // The `_.matches` iteratee shorthand.
  9. _.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
  10. // => objects for ['barney', 'fred']
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.dropRightWhile(users, ['active', false]);
  13. // => objects for ['barney']
  14. // The `_.property` iteratee shorthand.
  15. _.dropRightWhile(users, 'active');
  16. // => objects for ['barney', 'fred', 'pebbles']

_.dropWhile(array, [predicate=_.identity])

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).

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the slice of array.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': false },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': true }
  5. ];
  6. _.dropWhile(users, function(o) { return !o.active; });
  7. // => objects for ['pebbles']
  8. // The `_.matches` iteratee shorthand.
  9. _.dropWhile(users, { 'user': 'barney', 'active': false });
  10. // => objects for ['fred', 'pebbles']
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.dropWhile(users, ['active', false]);
  13. // => objects for ['pebbles']
  14. // The `_.property` iteratee shorthand.
  15. _.dropWhile(users, 'active');
  16. // => objects for ['barney', 'fred', 'pebbles']

_.fill(array, value, [start=0], [end=array.length])

Fills elements of array with value from start up to, but not including, end.

Note: This method mutates array.

Since

3.2.0

Arguments

  1. array (Array): The array to fill.
  2. value (*): The value to fill array with.
  3. [start=0] (number): The start position.
  4. [end=array.length] (number): The end position.

Returns

(Array): Returns array.

Example

  1. var array = [1, 2, 3];
  2. _.fill(array, 'a');
  3. console.log(array);
  4. // => ['a', 'a', 'a']
  5. _.fill(Array(3), 2);
  6. // => [2, 2, 2]
  7. _.fill([4, 6, 8, 10], '*', 1, 3);
  8. // => [4, '*', '*', 10]

_.findIndex(array, [predicate=_.identity], [fromIndex=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.

Since

1.1.0

Arguments

  1. array (Array): The array to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.
  3. [fromIndex=0] (number): The index to search from.

Returns

(number): Returns the index of the found element, else -1.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': false },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': true }
  5. ];
  6. _.findIndex(users, function(o) { return o.user == 'barney'; });
  7. // => 0
  8. // The `_.matches` iteratee shorthand.
  9. _.findIndex(users, { 'user': 'fred', 'active': false });
  10. // => 1
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.findIndex(users, ['active', false]);
  13. // => 0
  14. // The `_.property` iteratee shorthand.
  15. _.findIndex(users, 'active');
  16. // => 2

_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])

This method is like _.findIndex except that it iterates over elements of collection from right to left.

Since

2.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [predicate=_.identity] (Function): The function invoked per iteration.
  3. [fromIndex=array.length-1] (number): The index to search from.

Returns

(number): Returns the index of the found element, else -1.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': true },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': false }
  5. ];
  6. _.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
  7. // => 2
  8. // The `_.matches` iteratee shorthand.
  9. _.findLastIndex(users, { 'user': 'barney', 'active': true });
  10. // => 0
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.findLastIndex(users, ['active', false]);
  13. // => 2
  14. // The `_.property` iteratee shorthand.
  15. _.findLastIndex(users, 'active');
  16. // => 0

_.flatten(array)

Flattens array a single level deep.

Since

0.1.0

Arguments

  1. array (Array): The array to flatten.

Returns

(Array): Returns the new flattened array.

Example

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

_.flattenDeep(array)

Recursively flattens array.

Since

3.0.0

Arguments

  1. array (Array): The array to flatten.

Returns

(Array): Returns the new flattened array.

Example

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

_.flattenDepth(array, [depth=1])

Recursively flatten array up to depth times.

Since

4.4.0

Arguments

  1. array (Array): The array to flatten.
  2. [depth=1] (number): The maximum recursion depth.

Returns

(Array): Returns the new flattened array.

Example

  1. var array = [1, [2, [3, [4]], 5]];
  2. _.flattenDepth(array, 1);
  3. // => [1, 2, [3, [4]], 5]
  4. _.flattenDepth(array, 2);
  5. // => [1, 2, 3, [4], 5]

_.fromPairs(pairs)

The inverse of _.toPairs; this method returns an object composed from key-value pairs.

Since

4.0.0

Arguments

  1. pairs (Array): The key-value pairs.

Returns

(Object): Returns the new object.

Example

  1. _.fromPairs([['a', 1], ['b', 2]]);
  2. // => { 'a': 1, 'b': 2 }

_.head(array)

Gets the first element of array.

Since

0.1.0

Aliases

_.first

Arguments

  1. array (Array): The array to query.

Returns

(*): Returns the first element of array.

Example

  1. _.head([1, 2, 3]);
  2. // => 1
  3. _.head([]);
  4. // => undefined

_.indexOf(array, value, [fromIndex=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.

Since

0.1.0

Arguments

  1. array (Array): The array to inspect.
  2. value (*): The value to search for.
  3. [fromIndex=0] (number): The index to search from.

Returns

(number): Returns the index of the matched value, else -1.

Example

  1. _.indexOf([1, 2, 1, 2], 2);
  2. // => 1
  3. // Search from the `fromIndex`.
  4. _.indexOf([1, 2, 1, 2], 2, 2);
  5. // => 3

_.initial(array)

Gets all but the last element of array.

Since

0.1.0

Arguments

  1. array (Array): The array to query.

Returns

(Array): Returns the slice of array.

Example

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

_.intersection([arrays])

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.

Since

0.1.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.

Returns

(Array): Returns the new array of intersecting values.

Example

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

_.intersectionBy([arrays], [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new array of intersecting values.

Example

  1. _.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  2. // => [2.1]
  3. // The `_.property` iteratee shorthand.
  4. _.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  5. // => [{ 'x': 1 }]

_.intersectionWith([arrays], [comparator])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns the new array of intersecting values.

Example

  1. var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  2. var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  3. _.intersectionWith(objects, others, _.isEqual);
  4. // => [{ 'x': 1, 'y': 2 }]

_.join(array, [separator=','])

Converts all elements in array into a string separated by separator.

Since

4.0.0

Arguments

  1. array (Array): The array to convert.
  2. [separator=','] (string): The element separator.

Returns

(string): Returns the joined string.

Example

  1. _.join(['a', 'b', 'c'], '~');
  2. // => 'a~b~c'

_.last(array)

Gets the last element of array.

Since

0.1.0

Arguments

  1. array (Array): The array to query.

Returns

(*): Returns the last element of array.

Example

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

_.lastIndexOf(array, value, [fromIndex=array.length-1])

This method is like _.indexOf except that it iterates over elements of array from right to left.

Since

0.1.0

Arguments

  1. array (Array): The array to inspect.
  2. value (*): The value to search for.
  3. [fromIndex=array.length-1] (number): The index to search from.

Returns

(number): Returns the index of the matched value, else -1.

Example

  1. _.lastIndexOf([1, 2, 1, 2], 2);
  2. // => 3
  3. // Search from the `fromIndex`.
  4. _.lastIndexOf([1, 2, 1, 2], 2, 2);
  5. // => 1

_.nth(array, [n=0])

Gets the element at index n of array. If n is negative, the nth element from the end is returned.

Since

4.11.0

Arguments

  1. array (Array): The array to query.
  2. [n=0] (number): The index of the element to return.

Returns

(*): Returns the nth element of array.

Example

  1. var array = ['a', 'b', 'c', 'd'];
  2. _.nth(array, 1);
  3. // => 'b'
  4. _.nth(array, -2);
  5. // => 'c';

_.pull(array, [values])

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.

Since

2.0.0

Arguments

  1. array (Array): The array to modify.
  2. [values] (…*): The values to remove.

Returns

(Array): Returns array.

Example

  1. var array = ['a', 'b', 'c', 'a', 'b', 'c'];
  2. _.pull(array, 'a', 'c');
  3. console.log(array);
  4. // => ['b', 'b']

_.pullAll(array, values)

This method is like _.pull except that it accepts an array of values to remove.

Note: Unlike _.difference, this method mutates array.

Since

4.0.0

Arguments

  1. array (Array): The array to modify.
  2. values (Array): The values to remove.

Returns

(Array): Returns array.

Example

  1. var array = ['a', 'b', 'c', 'a', 'b', 'c'];
  2. _.pullAll(array, ['a', 'c']);
  3. console.log(array);
  4. // => ['b', 'b']

_.pullAllBy(array, values, [iteratee=_.identity])

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.

Since

4.0.0

Arguments

  1. array (Array): The array to modify.
  2. values (Array): The values to remove.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns array.

Example

  1. var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
  2. _.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
  3. console.log(array);
  4. // => [{ 'x': 2 }]

_.pullAllWith(array, values, [comparator])

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.

Since

4.6.0

Arguments

  1. array (Array): The array to modify.
  2. values (Array): The values to remove.
  3. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns array.

Example

  1. var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
  2. _.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
  3. console.log(array);
  4. // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]

_.pullAt(array, [indexes])

Removes elements from array corresponding to indexes and returns an array of removed elements.

Note: Unlike _.at, this method mutates array.

Since

3.0.0

Arguments

  1. array (Array): The array to modify.
  2. [indexes] (…(number|number[])): The indexes of elements to remove.

Returns

(Array): Returns the new array of removed elements.

Example

  1. var array = ['a', 'b', 'c', 'd'];
  2. var pulled = _.pullAt(array, [1, 3]);
  3. console.log(array);
  4. // => ['a', 'c']
  5. console.log(pulled);
  6. // => ['b', 'd']

_.remove(array, [predicate=_.identity])

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.

Since

2.0.0

Arguments

  1. array (Array): The array to modify.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the new array of removed elements.

Example

  1. var array = [1, 2, 3, 4];
  2. var evens = _.remove(array, function(n) {
  3. return n % 2 == 0;
  4. });
  5. console.log(array);
  6. // => [1, 3]
  7. console.log(evens);
  8. // => [2, 4]

_.reverse(array)

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.

Since

4.0.0

Arguments

  1. array (Array): The array to modify.

Returns

(Array): Returns array.

Example

  1. var array = [1, 2, 3];
  2. _.reverse(array);
  3. // => [3, 2, 1]
  4. console.log(array);
  5. // => [3, 2, 1]

_.slice(array, [start=0], [end=array.length])

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.

Since

3.0.0

Arguments

  1. array (Array): The array to slice.
  2. [start=0] (number): The start position.
  3. [end=array.length] (number): The end position.

Returns

(Array): Returns the slice of array.

_.sortedIndex(array, value)

Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.

Since

0.1.0

Arguments

  1. array (Array): The sorted array to inspect.
  2. value (*): The value to evaluate.

Returns

(number): Returns the index at which value should be inserted into array.

Example

  1. _.sortedIndex([30, 50], 40);
  2. // => 1

_.sortedIndexBy(array, value, [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. array (Array): The sorted array to inspect.
  2. value (*): The value to evaluate.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(number): Returns the index at which value should be inserted into array.

Example

  1. var objects = [{ 'x': 4 }, { 'x': 5 }];
  2. _.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
  3. // => 0
  4. // The `_.property` iteratee shorthand.
  5. _.sortedIndexBy(objects, { 'x': 4 }, 'x');
  6. // => 0

_.sortedIndexOf(array, value)

This method is like _.indexOf except that it performs a binary search on a sorted array.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. value (*): The value to search for.

Returns

(number): Returns the index of the matched value, else -1.

Example

  1. _.sortedIndexOf([4, 5, 5, 5, 6], 5);
  2. // => 1

_.sortedLastIndex(array, value)

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.

Since

3.0.0

Arguments

  1. array (Array): The sorted array to inspect.
  2. value (*): The value to evaluate.

Returns

(number): Returns the index at which value should be inserted into array.

Example

  1. _.sortedLastIndex([4, 5, 5, 5, 6], 5);
  2. // => 4

_.sortedLastIndexBy(array, value, [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. array (Array): The sorted array to inspect.
  2. value (*): The value to evaluate.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(number): Returns the index at which value should be inserted into array.

Example

  1. var objects = [{ 'x': 4 }, { 'x': 5 }];
  2. _.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
  3. // => 1
  4. // The `_.property` iteratee shorthand.
  5. _.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
  6. // => 1

_.sortedLastIndexOf(array, value)

This method is like _.lastIndexOf except that it performs a binary search on a sorted array.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. value (*): The value to search for.

Returns

(number): Returns the index of the matched value, else -1.

Example

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

_.sortedUniq(array)

This method is like _.uniq except that it’s designed and optimized for sorted arrays.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.

Returns

(Array): Returns the new duplicate free array.

Example

  1. _.sortedUniq([1, 1, 2]);
  2. // => [1, 2]

_.sortedUniqBy(array, [iteratee])

This method is like _.uniqBy except that it’s designed and optimized for sorted arrays.

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [iteratee] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new duplicate free array.

Example

  1. _.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
  2. // => [1.1, 2.3]

_.tail(array)

Gets all but the first element of array.

Since

4.0.0

Arguments

  1. array (Array): The array to query.

Returns

(Array): Returns the slice of array.

Example

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

_.take(array, [n=1])

Creates a slice of array with n elements taken from the beginning.

Since

0.1.0

Arguments

  1. array (Array): The array to query.
  2. [n=1] (number): The number of elements to take.

Returns

(Array): Returns the slice of array.

Example

  1. _.take([1, 2, 3]);
  2. // => [1]
  3. _.take([1, 2, 3], 2);
  4. // => [1, 2]
  5. _.take([1, 2, 3], 5);
  6. // => [1, 2, 3]
  7. _.take([1, 2, 3], 0);
  8. // => []

_.takeRight(array, [n=1])

Creates a slice of array with n elements taken from the end.

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [n=1] (number): The number of elements to take.

Returns

(Array): Returns the slice of array.

Example

  1. _.takeRight([1, 2, 3]);
  2. // => [3]
  3. _.takeRight([1, 2, 3], 2);
  4. // => [2, 3]
  5. _.takeRight([1, 2, 3], 5);
  6. // => [1, 2, 3]
  7. _.takeRight([1, 2, 3], 0);
  8. // => []

_.takeRightWhile(array, [predicate=_.identity])

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).

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the slice of array.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': true },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': false }
  5. ];
  6. _.takeRightWhile(users, function(o) { return !o.active; });
  7. // => objects for ['fred', 'pebbles']
  8. // The `_.matches` iteratee shorthand.
  9. _.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
  10. // => objects for ['pebbles']
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.takeRightWhile(users, ['active', false]);
  13. // => objects for ['fred', 'pebbles']
  14. // The `_.property` iteratee shorthand.
  15. _.takeRightWhile(users, 'active');
  16. // => []

_.takeWhile(array, [predicate=_.identity])

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).

Since

3.0.0

Arguments

  1. array (Array): The array to query.
  2. [predicate=_.identity] (Function): The function invoked per iteration.

Returns

(Array): Returns the slice of array.

Example

  1. var users = [
  2. { 'user': 'barney', 'active': false },
  3. { 'user': 'fred', 'active': false },
  4. { 'user': 'pebbles', 'active': true }
  5. ];
  6. _.takeWhile(users, function(o) { return !o.active; });
  7. // => objects for ['barney', 'fred']
  8. // The `_.matches` iteratee shorthand.
  9. _.takeWhile(users, { 'user': 'barney', 'active': false });
  10. // => objects for ['barney']
  11. // The `_.matchesProperty` iteratee shorthand.
  12. _.takeWhile(users, ['active', false]);
  13. // => objects for ['barney', 'fred']
  14. // The `_.property` iteratee shorthand.
  15. _.takeWhile(users, 'active');
  16. // => []

_.union([arrays])

Creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons.

Since

0.1.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.

Returns

(Array): Returns the new array of combined values.

Example

  1. _.union([2], [1, 2]);
  2. // => [2, 1]

_.unionBy([arrays], [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new array of combined values.

Example

  1. _.unionBy([2.1], [1.2, 2.3], Math.floor);
  2. // => [2.1, 1.2]
  3. // The `_.property` iteratee shorthand.
  4. _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  5. // => [{ 'x': 1 }, { 'x': 2 }]

_.unionWith([arrays], [comparator])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns the new array of combined values.

Example

  1. var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  2. var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  3. _.unionWith(objects, others, _.isEqual);
  4. // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

_.uniq(array)

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.

Since

0.1.0

Arguments

  1. array (Array): The array to inspect.

Returns

(Array): Returns the new duplicate free array.

Example

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

_.uniqBy(array, [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new duplicate free array.

Example

  1. _.uniqBy([2.1, 1.2, 2.3], Math.floor);
  2. // => [2.1, 1.2]
  3. // The `_.property` iteratee shorthand.
  4. _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  5. // => [{ 'x': 1 }, { 'x': 2 }]

_.uniqWith(array, [comparator])

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).

Since

4.0.0

Arguments

  1. array (Array): The array to inspect.
  2. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns the new duplicate free array.

Example

  1. var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
  2. _.uniqWith(objects, _.isEqual);
  3. // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

_.unzip(array)

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.

Since

1.2.0

Arguments

  1. array (Array): The array of grouped elements to process.

Returns

(Array): Returns the new array of regrouped elements.

Example

  1. var zipped = _.zip(['a', 'b'], [1, 2], [true, false]);
  2. // => [['a', 1, true], ['b', 2, false]]
  3. _.unzip(zipped);
  4. // => [['a', 'b'], [1, 2], [true, false]]

_.unzipWith(array, [iteratee=_.identity])

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).

Since

3.8.0

Arguments

  1. array (Array): The array of grouped elements to process.
  2. [iteratee=_.identity] (Function): The function to combine regrouped values.

Returns

(Array): Returns the new array of regrouped elements.

Example

  1. var zipped = _.zip([1, 2], [10, 20], [100, 200]);
  2. // => [[1, 10, 100], [2, 20, 200]]
  3. _.unzipWith(zipped, _.add);
  4. // => [3, 30, 300]

_.without(array, [values])

Creates an array excluding all given values using SameValueZero for equality comparisons.

Note: Unlike _.pull, this method returns a new array.

Since

0.1.0

Arguments

  1. array (Array): The array to inspect.
  2. [values] (…*): The values to exclude.

Returns

(Array): Returns the new array of filtered values.

Example

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

_.xor([arrays])

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.

Since

2.4.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.

Returns

(Array): Returns the new array of filtered values.

Example

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

_.xorBy([arrays], [iteratee=_.identity])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Array): Returns the new array of filtered values.

Example

  1. _.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  2. // => [1.2, 3.4]
  3. // The `_.property` iteratee shorthand.
  4. _.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  5. // => [{ 'x': 2 }]

_.xorWith([arrays], [comparator])

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).

Since

4.0.0

Arguments

  1. [arrays] (…Array): The arrays to inspect.
  2. [comparator] (Function): The comparator invoked per element.

Returns

(Array): Returns the new array of filtered values.

Example

  1. var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  2. var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  3. _.xorWith(objects, others, _.isEqual);
  4. // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

_.zip([arrays])

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.

Since

0.1.0

Arguments

  1. [arrays] (…Array): The arrays to process.

Returns

(Array): Returns the new array of grouped elements.

Example

  1. _.zip(['a', 'b'], [1, 2], [true, false]);
  2. // => [['a', 1, true], ['b', 2, false]]

_.zipObject([props=[]], [values=[]])

This method is like _.fromPairs except that it accepts two arrays, one of property identifiers and one of corresponding values.

Since

0.4.0

Arguments

  1. [props=[]] (Array): The property identifiers.
  2. [values=[]] (Array): The property values.

Returns

(Object): Returns the new object.

Example

  1. _.zipObject(['a', 'b'], [1, 2]);
  2. // => { 'a': 1, 'b': 2 }

_.zipObjectDeep([props=[]], [values=[]])

This method is like _.zipObject except that it supports property paths.

Since

4.1.0

Arguments

  1. [props=[]] (Array): The property identifiers.
  2. [values=[]] (Array): The property values.

Returns

(Object): Returns the new object.

Example

  1. _.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
  2. // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }

_.zipWith([arrays], [iteratee=_.identity])

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).

Since

3.8.0

Arguments

  1. [arrays] (…Array): The arrays to process.
  2. [iteratee=_.identity] (Function): The function to combine grouped values.

Returns

(Array): Returns the new array of grouped elements.

Example

  1. _.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
  2. return a + b + c;
  3. });
  4. // => [111, 222]