Duca
Duca

Reputation: 73

sum values of two different objects

I have two different objects

var objA = { a: 10, b: 20, c: 30 }; var objB = { a: 3, c: 6, d: 3 };

and I want to sum their values , like in this example below

{ a: 13, b: 20, c: 36, d: 3 }

Upvotes: 4

Views: 2643

Answers (5)

Pranav C Balan
Pranav C Balan

Reputation: 115272

Use Array#reduce method

var objA = {
  a: 10,
  b: 20,
  c: 30
};
var objB = {
  a: 3,
  c: 6,
  d: 3
};

console.log(
  // concatenate keys array
  Object.keys(objA).concat(Object.keys(objB))
  // or use Object.keys(Object.assign({},objA,objB))

  // iterate to generate the object
  .reduce(function(obj, k) {
    // define object property, treat as 0 if not defined
    obj[k] = (objA[k] || 0) + (objB[k] || 0);
    // return object difference
    return obj;
    // set initial value as an empty object
  }, {})
)


Or much better way by defining initial value as the copy of second object.

var objA = {
  a: 10,
  b: 20,
  c: 30
};
var objB = {
  a: 3,
  c: 6,
  d: 3
};

console.log(
  // get property names array of objB ----Edit objA -----
  Object.keys(objA)
  // iterate over array
  .reduce(function(obj, k) {
    // add value to existing property or update
    obj[k] = (obj[k] || 0) + objA[k];
    // return object reference
    return obj;
    // define initial value as an object which holds 
    // all the property and value in objB
  }, Object.assign({}, objB))
)

Upvotes: 8

karina
karina

Reputation: 829

I like very much second Pranav C Balan solution. But i waste a little time so also post my solution :).

var objA = { a: 10, b: 20, c: 30 };
var objB = { a: 3, c: 6, d: 3 };


let params = Array.from(new Set(Object.keys(objA).concat(Object.keys(objB))));
let sum = params.map(param => (objA[param] || 0) + (objB[param] || 0));

console.log(params, sum);

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

Sort solution using Object.assign() and Array.prototype.reduce() functions:

var objA = { a: 10, b: 20, c: 30 },
    objB = { a: 3, c: 6, d: 3},
    merged = Object.assign({}, objA, objB);

var result = Object.keys(merged).reduce(function (r, k) {
    r[k] = (merged[k] && objA[k] && objB[k])? merged[k] + objA[k] : merged[k];
    return r;
}, {});

console.log(result);

Upvotes: 0

AshBringer
AshBringer

Reputation: 2673

Using lodash

function customizer(objValue, srcValue) {
    if(objValue && srcValue)
    return objValue+srcValue;
}

var objA = { a: 10, b: 20, c: 30 }; var objB = { a: 3, c: 6, d: 3 };

console.log(_.mergeWith(objA, objB, customizer));

Demo

Upvotes: 3

Potray
Potray

Reputation: 1998

var objA = { a: 10, b: 20, c: 30 }; var objB = { a: 3, c: 6, d: 3 };
var objC = {}
for (key in objA) {
	if (objB.hasOwnProperty(key)) {
		objC[key] = objA[key] + objB[key]
  } else {
  	objC[key] = objA[key]
  }
}
for (key in objB) {
	if (!objC.hasOwnProperty(key)) {
    if (objA.hasOwnProperty(key)) {
      objC[key] = objA[key] + objB[key]
    } else {
      objC[key] = objB[key]
    }
  }
}
console.log(objC)

Upvotes: 2

Related Questions