Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sum array values 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
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: Function used to reload the portion of a page using javascript 
Javascript :: change source of image jquery 
Javascript :: javascript check image src 
Javascript :: jquery add table row 
Javascript :: convert date to timestamp javascript 
Javascript :: validate mobile number in javascript 
Javascript :: regular expressions password contains number 
Javascript :: chalk nodeks 
Javascript :: Date gethours js 
Javascript :: joi object id validation 
Javascript :: get random item from array javascript 
Javascript :: debounchow use input debounce in javascript vue.js 
Javascript :: syntax for srcset in react 
Javascript :: javascript download current html page 
Javascript :: Razorpay generate Signature in the node js 
Javascript :: node mysql 
Javascript :: Regex get emojis 
Javascript :: question mark and colon in javascript 
Javascript :: every method javascript 
Javascript :: python json.dumps pretty print 
Javascript :: localdatetime json 
Javascript :: javascript to remove few items from array 
Javascript :: leaflet each layer 
Javascript :: how to set css variables in javascript 
Javascript :: difference between indexof and search in javascript 
Javascript :: axios.post headers example 
Javascript :: javascript kill ajax request 
Javascript :: datetime to date javascript 
Javascript :: using multiparty with node js express 
Javascript :: string compare on date in js 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =