Array.prototype.sum >= 0.1.3
Purpose
Create the sum of all the values inside the array
Syntax
Array#sum
(
Stringproperty
Functionmap
);
When no parameters are given, sum will add up all the entries in the array. This can result in a number or a string.
If a property name is given, said property of every element inside the array will be used for the sum.
If a map function is given, that function will receive the value as its only argument. The value that that function returns will be used for the sum.
Parameters
property
- The property name to use of all the values
map
- The map function to use
Return values
Number
String
Examples
Sum up an array of numbers
var arr = [0, 1, 2, 3];
arr.sum();>>> 6
Sum up a certain property
var arr = [0, 1, 2, 3];
arr.sum();>>> 6
Use a map function
var arr = ['a', 'b', 'c'],
sum;
arr.sum(function code(value) {
// `value` will be 'a', 'b', 'c' in order
return value.charCodeAt(0);
});>>> 294
Comments