Karthiga
Karthiga

Reputation: 897

Adding or appending object with object javascript

i am having 2 objects and the code is

Object 1

var existingValue=self.get('objHubDto').ftd.gridDataDTO.gridDTOMap[name].gridDataMap;

Object 2

var newValue =  self.get('childDataList')

I need to merge/add/append this two in newvalue value eventhough it having duplicate key values

Upvotes: 0

Views: 74

Answers (1)

arizafar
arizafar

Reputation: 3122

Following is a very basic example to merge by keeping both values, however, you can create new object rather mutating existing one.

let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = { a: 9, d: 8, e: 7 };

Object.entries(obj2).forEach(([key, val]) => {
	obj1[key] = obj1[key] ? [obj1[key], val] : val;
});

console.log(obj1);

Upvotes: 1

Related Questions