I have two simple objects like this.
var obj1 = {
"data": {
"Category": "OUTFLOWS",
"Opening": 3213.11,
"Mar16": 3213.12,
"Apr16": 3148.13,
"May16": 3148.14,
"Jun16": 3148.15,
"July16": 3148.16,
"Aug16": 3148.17,
"Sep16": 3148.18,
"Oct16": 3148.19,
"Nov16": 3148.21,
"Dec16": 3148.22,
"Jan17": 3148.23,
"Feb17": 3148.24,
"ClosingCash": 3544.25
}
}
var obj2 = {
"data": {
"Category": "INFLOWS",
"Opening": 2213.11,
"Mar16": 2213.12,
"Apr16": 2148.13,
"May16": 2148.14,
"Jun16": 2148.15,
"July16": 2148.16,
"Aug16": 2148.17,
"Sep16": 2148.18,
"Oct16": 2148.19,
"Nov16": 2148.21,
"Dec16": 2148.22,
"Jan17": 2148.23,
"Feb17": 2148.24,
"ClosingCash": 2544.25
}
}
Now i want to add this two object values and store this in another object. I can do this manually adding obj1.data.Jan17 + obj2.data.Jan17
and get the result.
But in some cases am getting a lot of data where manually doing is impossible. So i need to create a function to add this two object after matching the key and return a single object.
I tried with Object.keys()
to get the key of the object. But after doing the for loop it is throwing some error. My sample is code is like this.
let arrayKey = Object.keys(obj1);
for (var i in arrayKey){
obj1.data.arrayKey[i] = obj1.data.arrayKey[i] + obj2.data.arrayKey[i];
}
If anybody will help then it will be great.