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.
array (Array): The array to process. [size=1] (number): The length of each chunk
(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']]
Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.
array (Array): The array to compact.
(Array): Returns the new array of filtered values.
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
Creates a new array concatenating array with any additional arrays and/or values.
array (Array): The array to concatenate. [values] (...x): The values to concatenate.
(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]
Creates a slice of array with n elements dropped from the end.
array (Array): The array to query. [n=1] (number): The number of elements to drop.
(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]
Gets the first element of array.
array (Array): The array to query.
(x): Returns the first element of array.
_.head([1, 2, 3]);
// => 1
_.head([]);
// => undefined
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.
array (Array): The array to inspect. value (x): The value to search for. [fromIndex=0] (number): The index to search from.
(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
Gets all but the last element of array.
array (Array): The array to query.
(Array): Returns the slice of array.
_.initial([1, 2, 3]);
// => [1, 2]
Converts all elements in array into a string separated by separator.
array (Array): The array to convert. [separator=','] (string): The element separator.
(string): Returns the joined string.
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'
Gets the element at index n of array. If n is negative, the nth element from the end is returned.
array (Array): The array to query. [n=0] (number): The index of the element to return.
(x): Returns the nth element of array.
var array = ['a', 'b', 'c', 'd'];
_.nth(array, 1);
// => 'b'
_.nth(array, -2);
// => 'c';
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.
array (Array): The array to modify. [values] (...x): The values to remove.
(Array): Returns array.
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pull(array, 'a', 'c');
console.log(array);
// => ['b', 'b']
This method is like pull except that it accepts an array of values to remove.
Note: Unlike difference, this method mutates array.
array (Array): The array to modify. values (Array): The values to remove.
(Array): Returns array.
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pullAll(array, ['a', 'c']);
console.log(array);
// => ['b', 'b']
Removes elements from array corresponding to indexes and returns an array of removed elements.
Note: Unlike at, this method mutates array.
array (Array): The array to modify. [indexes] (...(number|number[])): The indexes of elements to remove.
(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']
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
array (Array): The array to modify.
(Array): Returns array.
var array = [1, 2, 3];
_.reverse(array);
// => [3, 2, 1]
console.log(array);
// => [3, 2, 1]
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.
array (Array): The array to slice. [start=0] (number): The start position. [end=array.length] (number): The end position.
(Array): Returns the slice of array.
var a = ['zero', 'one', 'two', 'three'];
var sliced = a.slice(1, 3);
console.log(a); // ['zero', 'one', 'two', 'three']
console.log(sliced); // ['one', 'two']
Checks if value is classified as a boolean primitive or object.
value (x): The value to check.
(boolean): Returns true if value is a boolean, else false.
_.isBoolean(false);
// => true
_.isBoolean(null);
// => false
Performs a deep comparison between two values to determine if they are equivalent.
value (x): The value to compare. other (x): The other value to compare.
(boolean): Returns true if the values are equivalent, else false.
var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);
// => true
object === other;
// => false
Checks if value is null.
value (x): The value to check.
(boolean): Returns true if value is null, else false.
_.isNull(null);
// => true
_.isNull(void 0);
// => false
Adds two numbers
augend (number): The first number in an addition.
addend (number): The second number in an addition.
(number): Returns the total.
_.add(6, 4);
// => 10
Divides two numbers
dividend (number): The first number in a division.
divisor (number): The second number in a division.
(number): Returns the quotient.
_.divide(6, 4);
// => 1.5
Computes the maximum value of array. If array is empty or falsey, undefined is returned.
array (Array): The array to iterate over.
(x): Returns the maximum value.
_.max([4, 2, 8, 6]);
// => 8
_.max([]);
// => undefined
This method is like .max except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked. The iteratee is invoked with one argument: (value).
array (Array): The array to iterate over.
[iteratee=.identity] (Function): The iteratee invoked per element.
Returns the maximum value.
var objects = [{ 'n': 1 }, { 'n': 2 }];
_.maxBy(objects, function(o) { return o.n; });
// => { 'n': 2 }
// The `_.property` iteratee shorthand.
_.maxBy(objects, 'n');
// => { 'n': 2 }
Computes the mean of the values in array.
array (Array): The array to iterate over.
(number): Returns the mean.
_.mean([4, 2, 8, 6]);
// => 5
Computes the minimum value of array. If array is empty or falsey, undefined is returned.
array (Array): The array to iterate over.
(x): Returns the minimum value.
_.min([4, 2, 8, 6]);
// => 2
_.min([]);
// => undefined
Multiply two numbers.
multiplier (number): The first number in a multiplication.
multiplicand (number): The second number in a multiplication.
(number): Returns the product.
_.multiply(6, 4);
// => 24
Subtract two numbers.
minuend (number): The first number in a subtraction.
subtrahend (number): The second number in a subtraction.
(number): Returns the difference.
_.subtract(6, 4);
// => 2
Computes the sum of the values in array.
array (Array): The array to iterate over.
(number): Returns the sum.
_.sum([4, 2, 8, 6]);
// => 20
Checks if string starts with the given target string.
[string=''] (string): The string to inspect.
[target] (string): The string to search for.
[position=0] (number): The position to search from.
(boolean): Returns true if string starts with target, else false.
_.startsWith('abc', 'a');
// => true
_.startsWith('abc', 'b');
// => false
_.startsWith('abc', 'b', 1);
// => true
Removes leading and trailing whitespace or specified characters from string.
[string=''] (string): The string to trim.
[chars=whitespace] (string): The characters to trim.
(string): Returns the trimmed string.
_.trim(' abc ');
// => 'abc'
_.trim('-_-abc-_-', '_-');
// => 'abc'
_.map([' foo ', ' bar '], _.trim);
// => ['foo', 'bar']