1

I'm having 2 sets of object, an example:

var objA = {};
 objA.data1a = 100;
 objA.data2a = 70;
 objA.data3a = 16;
 objA.data4a = 37;
 objA.data5a = 88;
 //my ObjA will contain the following
 //{"data1a":100, "data2a":70, ......... "data5a":88}
var objB = {}
 objA.data1b = 19;
 objA.data2b = 5;
 objA.data3b = 7;
 objA.data4b = 6;
 objA.data5b = 2;
var getDifferences = {};
//I want to get the differences using a loop but I'm not sure how am I able to do so.
//I am able to do this currently
// getDifferences.data1 = objA.data1a - objB.data1b;
// getDifferences.data2 = objA.data2a - objB.data2b;
// getDifferences.data3 = objA.data3a - objB.data3b;

may I know if there's a loop to loop through an object so that for each loop I can minus and store the differences into another object?

asked Apr 29, 2016 at 6:57
1
  • 2
    you may consider a better iterable structure like an array and not an object Commented Apr 29, 2016 at 6:58

2 Answers 2

2

An iteration over the keys. I assume, that objB should contain the other values.

var objA = { data1a: 100, data2a: 70, data3a: 16, data4a: 37, data5a: 88 },
 objB = { data1b: 19, data2b: 5, data3b: 7, data4b: 6, data5b: 2 },
 getDifferences = {};
Object.keys(objA).forEach(function (k) {
 var kk = k.substring(0, k.length - 1);
 getDifferences[kk] = objA[kk + 'a'] - objB[kk + 'b'];
});
document.write('<pre>' + JSON.stringify(getDifferences, 0, 4) + '</pre>');

More Example

What if.. my naming were different? EXAMPLE: var storeA = {}; {"1000": 532, "2020": 123} and var storeB = {}; {"1000": 200, "2020": 12}

var storeA = {"1000": 532, "2020": 123},
 storeB = {"1000": 200, "2020": 12},
 getDifferences = {};
Object.keys(storeA).forEach(function (k) {
 getDifferences[k] = storeA[k] - storeB[k];
});
document.write('<pre>' + JSON.stringify(getDifferences, 0, 4) + '</pre>');

Even more Example

in storeX obj and storeY obj. { '4100': 3060431, '4130': 4413045, '16386': 4191921, '17476': 4161761 } { '4100': 3332286, '4130': 7640173, '16386': 5773080, '17476': 4692493 }

var storeA = { '4100': 3060431, '4130': 4413045, '16386': 4191921, '17476': 4161761 },
 storeB = { '4100': 3332286, '4130': 7640173, '16386': 5773080, '17476': 4692493 },
 getDifferences = {};
Object.keys(storeA).forEach(function (k) {
 getDifferences[k] = Math.abs(storeA[k] - storeB[k]);
});
document.write('<pre>' + JSON.stringify(getDifferences, 0, 4) + '</pre>');

answered Apr 29, 2016 at 7:07
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. What if.. my naming were different? EXAMPLE: var storeA = {}; {"1000": 532, "2020": 123} and var storeB = {}; {"1000": 200, "2020": 12}
then you could map it directly.
I think it will be easier if I just list down the exact data I have.
I have this 2 set of data which i'm trying to get it to dynamically stores the differences.
in storeX obj and storeY obj. { '4100': 3060431, '4130': 4413045, '16386': 4191921, '17476': 4161761 } { '4100': 3332286, '4130': 7640173, '16386': 5773080, '17476': 4692493 }
|
0

Yes, you actually can do this using Object.keys() function. Here is my example to do this:

var objA = {};
 objA.data1a = 100;
 objA.data2a = 70;
 objA.data3a = 16;
 objA.data4a = 37;
 objA.data5a = 88;
 //my ObjA will contain the following
 //{"data1a":100, "data2a":70, ......... "data5a":88}
var objB = {}
 objB.data1b = 19;
 objB.data2b = 5;
 objB.data3b = 7;
 objB.data4b = 6;
 objB.data5b = 2;
var getDifferences = {};
/// =================
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length == keysB.length) {
 for (i = 0; i < keysA.length; i++) {
 getDifferences['data'+i] = objA[keysA[i]] - objB[keysB[i]];
 }
}
console.log(getDifferences);
answered Apr 29, 2016 at 7:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.