Array.prototype.sum >= 0.1.3

Purpose

Create the sum of all the values inside the array

Description

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.

Syntax

Array#sum ( StringpropertyFunctionmap );

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

Margie

I found your web site from Google and I need to say it was a terrific locate. Thanks!

Eddy

Do you have any type of suggestions for writing posts? That's where I constantly struggle and I simply finish up gazing vacant screen for long period of time.

Zora

Do you have any type of suggestions for creating write-ups? That's where I always struggle and I just end up looking vacant screen for very long time.