locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
30 lines (29 loc) • 1.02 kB
JavaScript
import { toPhpArrayObject } from "../_helpers/_phpTypes.js";
export function array_sum(array) {
// discuss at: https://locutus.io/php/array_sum/
// original by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Nate
// bugfixed by: Gilbert
// improved by: David Pilia (https://www.beteck.it/)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: array_sum([4, 9, 182.6])
// returns 1: 195.6
// example 2: var $total = []
// example 2: var $index = 0.1
// example 2: for (var $y = 0; $y < 12; $y++){ $total[$y] = $y + $index }
// example 2: array_sum($total)
// returns 2: 67.2
let sum = 0;
// input sanitation
if (array === null || typeof array !== 'object') {
return null;
}
const values = toPhpArrayObject(array);
for (const value of Object.values(values)) {
const parsed = parseFloat(String(value));
if (!isNaN(parsed)) {
sum += parsed;
}
}
return sum;
}