Math methods perform arithmetic and aggregation, including collection variants such as sumBy and meanBy that accept an iteratee.
_.add(augend, addend)
Available since v3.4.0.
Adds two numbers.
| Parameter | Type | Description |
|---|---|---|
augend | number | The first number in an addition. |
addend | number | The second number in an addition. |
Returns number — Returns the total.
_.add(6, 4);
// => 10_.ceil(number, [precision=0])
Available since v3.10.0.
Computes number rounded up to precision.
| Parameter | Type | Description |
|---|---|---|
number | number | The number to round up. |
[precision=0] | number | The precision to round up to. |
Returns number — Returns the rounded up number.
_.ceil(4.006);
// => 5
_.ceil(6.004, 2);
// => 6.01
_.ceil(6040, -2);
// => 6100_.divide(dividend, divisor)
Available since v4.7.0.
Divide two numbers.
| Parameter | Type | Description |
|---|---|---|
dividend | number | The first number in a division. |
divisor | number | The second number in a division. |
Returns number — Returns the quotient.
_.divide(6, 4);
// => 1.5_.floor(number, [precision=0])
Available since v3.10.0.
Computes number rounded down to precision.
| Parameter | Type | Description |
|---|---|---|
number | number | The number to round down. |
[precision=0] | number | The precision to round down to. |
Returns number — Returns the rounded down number.
_.floor(4.006);
// => 4
_.floor(0.046, 2);
// => 0.04
_.floor(4060, -2);
// => 4000_.max(array)
Available since v0.1.0.
Computes the maximum value of array. If array is empty or falsey, undefined is returned.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
Returns any — Returns the maximum value.
_.max([4, 2, 8, 6]);
// => 8
_.max([]);
// => undefined_.maxBy(array, [iteratee=identity])
Available since v4.0.0.
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).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns any — 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 }_.mean(array)
Available since v4.0.0.
Computes the mean of the values in array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
Returns number — Returns the mean.
_.mean([4, 2, 8, 6]);
// => 5_.meanBy(array, [iteratee=identity])
Available since v4.7.0.
This method is like _.mean except that it accepts iteratee which is invoked for each element in array to generate the value to be averaged. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns number — Returns the mean.
var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
_.meanBy(objects, function(o) { return o.n; });
// => 5
// The `_.property` iteratee shorthand.
_.meanBy(objects, 'n');
// => 5_.min(array)
Available since v0.1.0.
Computes the minimum value of array. If array is empty or falsey, undefined is returned.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
Returns any — Returns the minimum value.
_.min([4, 2, 8, 6]);
// => 2
_.min([]);
// => undefined_.minBy(array, [iteratee=identity])
Available since v4.0.0.
This method is like _.min 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).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns any — Returns the minimum value.
var objects = [{ 'n': 1 }, { 'n': 2 }];
_.minBy(objects, function(o) { return o.n; });
// => { 'n': 1 }
// The `_.property` iteratee shorthand.
_.minBy(objects, 'n');
// => { 'n': 1 }_.multiply(multiplier, multiplicand)
Available since v4.7.0.
Multiply two numbers.
| Parameter | Type | Description |
|---|---|---|
multiplier | number | The first number in a multiplication. |
multiplicand | number | The second number in a multiplication. |
Returns number — Returns the product.
_.multiply(6, 4);
// => 24_.round(number, [precision=0])
Available since v3.10.0.
Computes number rounded to precision.
| Parameter | Type | Description |
|---|---|---|
number | number | The number to round. |
[precision=0] | number | The precision to round to. |
Returns number — Returns the rounded number.
_.round(4.006);
// => 4
_.round(4.006, 2);
// => 4.01
_.round(4060, -2);
// => 4100_.subtract(minuend, subtrahend)
Available since v4.0.0.
Subtract two numbers.
| Parameter | Type | Description |
|---|---|---|
minuend | number | The first number in a subtraction. |
subtrahend | number | The second number in a subtraction. |
Returns number — Returns the difference.
_.subtract(6, 4);
// => 2_.sum(array)
Available since v3.4.0.
Computes the sum of the values in array.
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
Returns number — Returns the sum.
_.sum([4, 2, 8, 6]);
// => 20_.sumBy(array, [iteratee=identity])
Available since v4.0.0.
This method is like _.sum except that it accepts iteratee which is invoked for each element in array to generate the value to be summed. The iteratee is invoked with one argument: (value).
| Parameter | Type | Description |
|---|---|---|
array | Array | The array to iterate over. |
[iteratee=_.identity] | Function | The iteratee invoked per element. |
Returns number — Returns the sum.
var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
_.sumBy(objects, function(o) { return o.n; });
// => 20
// The `_.property` iteratee shorthand.
_.sumBy(objects, 'n');
// => 20