js get sum by key

var array = [
  {name: "Peter", age: 43},
  {name: "John", age: 32},
  {name: "Jake", age: 21}
];

array.reduce(function(sum, current) {
  return sum + current.age;
}, 0); // 43 + 32 + 21 = 96

0
2
Wealhtheow 95 points

                                    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

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
array sum using all key sum one key in javascript js get sum by key how to map by key js sum array sum by key javascript sum by key javascript array node js array sum from key value add sum key value javascript array sum of key value in an array js javascript sum value from key sum specified keys in array javascript js sum by key js sum any key in array javascript sum array by key javascript sum by key javasript get total sum of key sum values of same key array javascript js sum items of array where key matched js array sum by key how to sum elements in array javascript by key map sum item value by key javascript sum of array keys elements in javascript sum array by key javascript sum value in same key of array in javascript sum by key javascript js sum array with key javascript array sum by key get sum of array key value pairs javascript find key value pair sum in array javascript sum values in array by key in js sum array key values javascript sum of values based on key js how to do sum of key inside array elements in javascript sum keys in array javascript array with key pair value javascript sum sum a key of array of objects javascript sum equal keys sum array of objects into one object duplicate keys js sum array of objects duplicate keys sum array of objects key value javascript how to sum values in array of objects javascript with same name nodejs map over array of objects and add up all the numbers get sum of a key in array of objects javascript add up totals of key/value pair array of objects calc object values in array javascript sum object value in array if another fields is the same js count key value pair sum value calculate totals of key in object if another key contains text javascript? javascript sum object property values based on property javascript sum objects propbetry based on property javascript compare objects values and get sum by key javascript pass object keys sum array total price reduce inside object js sum array of objects if exists how to sum multiple object with the same key in an array? sum of all objects key in array array find sum of key in array of objects map key value javascript sum how to get the sum of Javascript key values pair set of only values sum of particular objects values in an array sum values of object javascript if key is same javascript sum values of object javascript if key is same all up all key values of objects in array javascript javascript sum keys value objects with same key sum keys in array of objects js how to sum the same array property name in javascript sum value of key in an array javascript for key in sum javascript sum "key value" and value array javascript sum key and value array javascript sum all key value in array of objects javascript sum key array object sum all keys in array of objects javacript sum array key how to access array of objects in javascript get key sum how to add total values of duplicate values of objects in an array how to add total values of recurring values of objects in an array array sum operation with array of object key value loop through array of objects and sum key value pairs javascript javascrip caculate the sun of an arry with objects by key value and return an object array key value sum using js map how to access any key in object to sum values using javascript javascript sum of key object properties in array get sum of array object key javascript sum array of objects by key javascript adding up values of keys in array keys javascript adding up values in array keys javascript js sum array of objects by key array sum of object keys javascript array object set same value in a common key array of objects multiple keys with same name check for a property in object and sum up other property values javascript sum all the same key values in array of objects sum of key array object in javascript compare key name and sum array of objects sum list values with same key javascript how to add push elements with same key in array of objects typescript take array and map by sum of key value adding the total of an key value pair object array in javascript javascript sum array of objects by key javascript sum array values by key
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source