mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 08:37:49 +00:00
Bump to v3.4.0.
This commit is contained in:
33
math/sum.js
Normal file
33
math/sum.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import isArray from '../lang/isArray';
|
||||
import toIterable from '../internal/toIterable';
|
||||
|
||||
/**
|
||||
* Gets the sum of the values in `collection`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Math
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @returns {number} Returns the sum.
|
||||
* @example
|
||||
*
|
||||
* _.sum([4, 6, 2]);
|
||||
* // => 12
|
||||
*
|
||||
* _.sum({ 'a': 4, 'b': 6, 'c': 2 });
|
||||
* // => 12
|
||||
*/
|
||||
function sum(collection) {
|
||||
if (!isArray(collection)) {
|
||||
collection = toIterable(collection);
|
||||
}
|
||||
var length = collection.length,
|
||||
result = 0;
|
||||
|
||||
while (length--) {
|
||||
result += +collection[length] || 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default sum;
|
||||
Reference in New Issue
Block a user