Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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