简体   繁体   中英

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM