Answers for "add up totals of key/value pair array of objects"

5

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
Posted by: Guest on May-22-2020
0

add key value pair to all objects in array

var arrOfObj = [{
  name: 'eve'
}, {
  name: 'john'
}, {
  name: 'jane'
}];

var result = arrOfObj.map(function(el) {
  var o = Object.assign({}, el);
  o.isActive = true;
  return o;
})

console.log(arrOfObj);
console.log(result);
Posted by: Guest on October-07-2020

Code answers related to "add up totals of key/value pair array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language