0

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.

Kishan
  • 350
  • 2
  • 10
  • 31

3 Answers3

1

This cycles the obj1 keys and if they exist in obj2 as well they get summed and set on obj3

var obj3 = {data: {}};
for(var k in obj1.data){
    if(obj2.data[k]){
        obj3.data[k] = obj1.data[k] + obj2.data[k];
    }
}
Luca De Nardi
  • 2,280
  • 16
  • 35
  • `i` is just not usual convention name. `i` usually stands for iterator. `k` is a better variable name. Also, for such basic posts, try to look for a relevant post and close them as dupe or share their links for others to close as you done have enough rep yet. – Rajesh Nov 02 '17 at 10:09
  • Thanks @Rajesh, will do – Luca De Nardi Nov 02 '17 at 10:10
1

Try 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
  }
}

var keys = Object.keys(obj1.data);
var obj3 = {};

keys.forEach(function(key) {
  obj3[key] = obj1.data[key] + obj2.data[key]
})
console.log(obj3);
Ozan
  • 3,709
  • 2
  • 18
  • 23
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
  • *Try this* is not an explanation. Please add proper explanation as to what and why have you changed? If there was any mistakes in OP's code, explain that as well. – Rajesh Nov 02 '17 at 10:14
0

There were a few problems with your code.

  • You were calling object.keys on the wrong object. You need to call it with the object that has the keys you want to iterate over: obj1.data;
  • You were trying to access properties like obj1.data.arrayKey[i]. That will try to access i property of object.data.arrayKey which is not defined. Use [] to access properties instead.

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
  }
}

let arrayKey = Object.keys(obj1.data);
for (var i in arrayKey) {
  obj1.data[arrayKey[i]] = obj1.data[arrayKey[i]] + obj2.data[arrayKey[i]];
}
console.log(obj1);
Ozan
  • 3,709
  • 2
  • 18
  • 23
  • An answer is incomplete without explanation. Please explain what and what have you changed? If there is any mistake in OP's code, explain that as well. Also, FYI, it is a bad practice to answer a dupe. – Rajesh Nov 02 '17 at 10:12
  • @Rajesh I posted it early by mistake, was editing to add explanations – Ozan Nov 02 '17 at 10:13