Answers for "js access object key and value and sum in object"

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

Code answers related to "js access object key and value and sum in object"

Code answers related to "Javascript"

Browse Popular Code Answers by Language