0

I have this JSON, I am trying to retrieve values by substituting variables as below.

var model = {
    regression : {
        regUtils : {
            subIDs : {
                B00099999823 : {
                    "a" : "b",
                    "c" : "d"               
            }
        }
    }
};



var vals = "B00099999823";
alert(model.regression.regUtils.subIDs.vals.a); // error returned : Object doesn't support this property or method

alert(model.regression.regUtils.subIDs.B00099999823.a); //alerts b

How can I substitute a variable in place of a key or object. thanks.

brso05
  • 13,142
  • 2
  • 21
  • 40
Ejaz
  • 1,504
  • 3
  • 25
  • 51

1 Answers1

2

You can access with a variable contents like this:

var vals = "B00099999823";
alert(model.regression.regUtils.subIDs[vals].a;

You can use [] to access an object by using a variables contents. It would be the same as this:

model.regression.regUtils.subIDs['B00099999823'].a
brso05
  • 13,142
  • 2
  • 21
  • 40