Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
 
PREVIOUS NEXT
Tagged: #js #sum #key
ADD COMMENT
Topic
Name
3+8 =