Returns the sum of each value in a list. A closure might be given which can be used as a shortcut for .map().sum()
.
This function does not generate a change.
array.sum([callback])
Argument | Type | Description |
---|---|---|
callback | closure (optional) | Use the callback on each element in the list and sum the return values. |
The sum of all values.
This code shows an example using sum():
[1, 4, 2, 9, 7, 8, 9, 3, 1].sum();
Return value in JSON format
44
Another example using a callback:
// Sum the x properties
[{x: 7, y: 1}, {x: 8, y: 5}].sum(|o| o.x);
15