onlycode.org

javascript
jquery
html
angularjs
css
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
sum all data in array of...

Sum all data in array of objects into new array of objects, just only code

lodash | arrays | javascript

edit
const _ = require('lodash')
let keys = ['costOfAirtickets', 'costOfHotel']; 
let results = _.zipObject(keys, keys.map(key => _.sum( _.map(data, key))))
...
{ costOfAirtickets: 4000, costOfHotel: 2200 }

35
Roopak A Nelliat, 24.09.2018 16:14




edit
_(data).flatMap(_.entries).groupBy('0').mapValues(v=>_.sumBy(v, '1')).value()

34
Koushik Chatterjee, 24.09.2018 16:38



edit
var data = [{costOfAirtickets: 2500, costOfHotel: 1200},{costOfAirtickets: 1500, costOfHotel: 1000}];
var sum ={};
for(var obj in data){
  for(var ele in data[obj]){
    if(!data[obj].hasOwnProperty(ele)) continue;
      if(sum[ele] === undefined){
        sum[ele] = data[obj][ele];
      }else{
        sum[ele] = sum[ele] + data[obj][ele];
      }
  }

}
var arr = [sum];
console.log(arr);

10
Armando Prieto, 24.09.2018 23:08

Add your just only code

your name:    



If you need full text click here

Some similar links Sum all data in array of objects into new array of objects ↓:
Sum of array object property values in new array of objects in Javascript

Sum similar keys in an array of objects

Sum javascript object propertyA values with same object propertyB in array of objects

Finding matching objects in an array of objects?



This project is created for those who just need only code, wihout maaaaaaaaaaany words.