Sum array of objects by unique keys I have an array of objects, each with a dimensions field for storing various metrics related to the object, e.g. length, width, weight, etc.  Each object may be different in terms of what metrics it has.

I have a solution but not sure if it's optimal.

let dimensions = [{ a: 1, b: 2, c: 3}, { a: 2, c: 1, d: 3 }, { b: 1, e: 8, f: 1 }]

// get all keys
let dimensionsFields = _.map(dimensions, Object.keys);
// =&gt; [[a, b, c], [a, c, d], [b, e, f]]

// flatten
dimensionsFields  = _.flatten(dimensionsFields );
// =&gt; [a, b, c, a, c, d, b, e, f]

// filter out duplicates
dimensionsFields  = _.uniq(dimensionsFields );
// =&gt; [a, b, c, d, e, f]

// init return data
let dimensionsData = {};

// loop through available metrics, add up the values
dimensionsFields.forEach(field =&gt; {
  dimensionsData[field] = _.reduce(dimensions, function(a, b) {
    return a + b[field];
  }, 0);
});
// =&gt; { a: 3, b: 3, c: 4, d: 3, e: 8, f: 1 }
What do you mean optimal?

Your thing transforms the object lots of times, which is liable to not be very efficient, if that's what you meant.

Here's something that I hope is simple to read that does your task.

function sumObjects(obj1, obj2) {
    return Object.keys(obj2).reduce(
        (acc, currKey) =&gt; ({
        ...acc,
        [currKey]: acc[currKey] ? acc[currKey] + obj2[currKey] : obj2[currKey]
        }),
        obj1
    );
}

const result = dimensions.reduce(sumObjects);


The observation is that you just need to see how to do your task for two objects and the rest is just a reduce.

The most efficient thing will likely be just a for loop, like 

const res = {};
for (const obj of dimensions) {
    for (const key in obj) {
        res[key] = res[key] ? res[key] + obj[key] : obj[key];
    }
}