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 :: write to file js 
Javascript :: javascript add event listener to all input 
Javascript :: how to get current screen name in react native 
Javascript :: javascript scroll to end of div 
Javascript :: css how to make a div scrollable 
Javascript :: angular for loop key value 
Javascript :: moment add 30 days 
Javascript :: reduce average javascript 
Javascript :: get age using moment 
Javascript :: NullInjectorError: No provider for HttpClient! 
Javascript :: gradlew clean in react native 
Javascript :: javascript get length of object 
Javascript :: chart js title 
Javascript :: border react native 
Javascript :: event listener mousemove 
Javascript :: javascript regex email 
Javascript :: react background image 
Javascript :: listen prop change vuejs 
Javascript :: how to loop audio in js 
Javascript :: generate random ip address javascript 
Javascript :: how to change my npm version 
Javascript :: get the placeholder value jquery 
Javascript :: navigate to url javascript 
Javascript :: convert 2 dimensional array to js object 
Javascript :: stop a video jquery 
Javascript :: javascript get hours difference between two dates 
Javascript :: mouse coordinates not math with canvas coordinates in js 
Javascript :: logic for building calculator using JavaScript without using eval 
Javascript :: delete cr 
Javascript :: set onclick javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =