javascript sum array values by key
var array = [
{name: "Peter", age: 43},
{name: "John", age: 32},
{name: "Jake", age: 21}
];
array.reduce(function(sum, current) {
return sum + current.age;
}, 0); // 43 + 32 + 21 = 96
javascript sum array values by key
var array = [
{name: "Peter", age: 43},
{name: "John", age: 32},
{name: "Jake", age: 21}
];
array.reduce(function(sum, current) {
return sum + current.age;
}, 0); // 43 + 32 + 21 = 96
javascript object total
function sum( obj ) {
var sum = 0;
for( var el in obj ) {
if( obj.hasOwnProperty( el ) ) {
sum += parseFloat( obj[el] );
}
}
return sum;
}
var sample = { a: 1 , b: 2 , c:3 };
var summed = sum( sample );
console.log( "sum: "+summed );
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us