Horje
js get sum by key Code Example
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
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




Javascript

Related
jimp get image size Code Example jimp get image size Code Example
stateprovider.js react Code Example stateprovider.js react Code Example
jquery get td value Code Example jquery get td value Code Example
javascript replace text within dom Code Example javascript replace text within dom Code Example
javascript store text file into string Code Example javascript store text file into string Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7