Use PHP functions in JavaScript

JavaScript array_sum

Returns the sum of the array entries

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
function array_sum (array) {
    // Returns the sum of the array entries  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/array_sum    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nate
    // +   bugfixed by: Gilbert
    // *     example 1: array_sum([4, 9, 182.6]);
    // *     returns 1: 195.6    // *     example 2: total = []; index = 0.1; for (y=0; y < 12; y++){total[y] = y + index;}
    // *     example 2: array_sum(total);
    // *     returns 2: 67.2
    var key, sum = 0;
        // input sanitation
    if (typeof array !== 'object') {
        return null;
    }
        for (key in array) {
        //tester_print_r(typeof sum);
        sum += (array[key] * 1);
    }
     return sum;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
array_sum([4, 9, 182.6]);

Should return

1
195.6

» Example 2

Running

1
2
total = []; index = 0.1; for (y=0; y < 12; y++){total[y] = y + index;}
array_sum(total);

Should return

1
67.2

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have array_sum goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
6 Jan '09 Permalink

q   @ Gilbert: Thanks for sharing! I think the problem was:

1
total = Array


instead of

1
total = new Array // or total = []


But a little extra input sanitation wouldn't hurt!

Gravatar
Gilbert
5 Jan '09 Permalink

q   I resolve my problem

Edit

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
function array_sum( array ) {
    // Calculate the sum of values in an array
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_sum/
    // +       version: 811.2517    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nate
    // *     example 1: array_sum([4, 9, 182.6]);
    // *     returns 1: 195.6
     var key, sum= parseInt(0);
 
    // input sanitation
    if( !array || (array.constructor !== Array &amp;&amp; array.constructor !== Object) || !array.length ){
        return null;    }
 
    for(key in array){
        sum += (array[key]) * 1 ;
    } 
    return sum;
}


1
2
3
4
56
7
8
totalPedidos = new Array;
index = 0.3;
 
        for(y=0; y &lt; 12; y++){
                totalPedidos[y] = y + index;        }
 
document.writeln(array_sum(totalPedidos));


Result = 69.6

Gravatar
Gilbert
5 Jan '09 Permalink

q   Hi!

I tryng this code

1
2
3
4
56
7
8
totalPedidos = new Array;
index = 0.1;
 
        for(y=0; y &lt; 12; y++){
                totalPedidos[y] = y * index;        }
 
document.writeln(array_sum(totalPedidos));


and return this >>
00.10.20.300000000000000040.40.50.60000000000000010.70000000000000010.80.911.1

Gravatar
Kevin van Zonneveld
25 Nov '08 Permalink

q  @ Nate: Thanks!

Gravatar
Nate
19 Nov '08 Permalink

q  The variable "key" is declared twice with "var". Probably the "var" in the for loop should be removed.

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ Nosredna: Thank you for your input. For many different reasons we've chosen to mimic PHP's behavior as much as possible. And as you can see: http://www.php.net/array_sum , PHP does not differentiate between numerically indexed arrays and 'associative arrays' (in JavaScript, known as objects). I hope this answers your question.

Gravatar
Nosredna
5 Aug '08 Permalink

q   I don't like that array_sum uses for/in. Makes the loop slow and leads to other properties of the array besides the numbered elements being picked up. For arrays, a normal for loop is more appropriate. I'm curious why for/in was chosen. To deal with sparse arrays? Anyway, seems like a bug to me.

1
2
3
4
arr=[1,2,3,4];
arr.other=5;
 
alert(array_sum(arr));


I'd prefer that alert 10 rather than 15.


Contribute a New function