Answers for "array of object total"

1

js get sum of array of objects

const getSumByKey = (arr, key) => {
  return arr.reduce((accumulator, current) => accumulator + Number(current[key]), 0)
}

arr = [{x:1}, {x:3}]
getSumByKey(arr, 'x') // returns 4
Posted by: Guest on June-04-2021
7

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 );
Posted by: Guest on April-23-2020

Code answers related to "array of object total"

Code answers related to "Javascript"

Browse Popular Code Answers by Language