I want to sum costs
with coefficients in done
object like this:
const costs = {a: 2000, b: 5000, c: 4000}
const done = {a: 1, b: 1, c: 2}
(1 * 2000) + (1 * 5000) + (2 * 4000) = 15000
I tried this with no luck:
function sumObjectsByKey(...objs) {
return objs.reduce((a, b) => {
for (let k in b) {
if (b.hasOwnProperty(k))
a[k] += (a[k] || 0) * b[k];
}
return a;
}, {});
}
console.log(sumObjectsByKey(obj1, obj2, obj3));
Any suggestions?
const costs = {a: 2000, b: 5000, c: 4000};
const done = {a: 1, b: 1, c: 2};
let total = 0;
for (let key in done) {
total += done[key] * costs[key];
}
console.log(total); // Output: 15000