phpjs
Version:
89 lines (73 loc) • 2.59 kB
Markdown
layout: page
title: "JavaScript array_sum function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_sum:339
- /functions/view/array_sum
- /functions/view/339
- /functions/array_sum:339
- /functions/339
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_sum
{% codeblock array/array_sum.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_sum.js raw on github %}
function array_sum (array) {
// From: http://phpjs.org/functions
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Nate
// + bugfixed by: Gilbert
// + improved by: David Pilia (http://www.beteck.it/)
// + improved by: Brett Zamir (http://brett-zamir.me)
// * 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;
if (array && typeof array === 'object' && array.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return array.sum.apply(array, Array.prototype.slice.call(arguments, 0));
}
// input sanitation
if (typeof array !== 'object') {
return null;
}
for (key in array) {
if (!isNaN(parseFloat(array[key]))) {
sum += parseFloat(array[key]);
}
}
return sum;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_sum.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_sum.js)
### Example 1
This code
{% codeblock lang:js example %}
array_sum([4, 9, 182.6]);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
195.6
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
total = []; index = 0.1; for (y=0; y < 12; y++){total[y] = y + index;}
array_sum(total);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
67.2
{% endcodeblock %}
### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}