Answers for "how to find sum of key value in object in js"

0

js get sum by key

var array = [
  {item: "Coffee", price: 4},
  {item: "Brownie", price: 5},
];

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

const total = getSumByKey(array, 'price') // 9
Posted by: Guest on July-28-2021
0

javascript sum array of objects by key

arr.reduce((total, obj) => obj.credit + total,0)
// 3
Posted by: Guest on September-10-2021

Code answers related to "how to find sum of key value in object in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language