I have an array of objects that looks like this:
var data = [{costOfAirtickets: 2500, costOfHotel: 1200},{costOfAirtickets: 1500, costOfHotel: 1000}]
Use Lodash to simplify your life.
const _ = require('lodash')
let keys = ['costOfAirtickets', 'costOfHotel'];
let results = _.zipObject(keys, keys.map(key => _.sum( _.map(data, key))))
...
{ costOfAirtickets: 4000, costOfHotel: 2200 }
Explanation:
_.sum(_.map(data, key)): generates sum of each array_.zipObject: zips the results with the array sumkeys.map() for sum of each key as _.map does not guarantee order.Documentation: