0

Let assume that I have the following json of config.

var config = {
   "type_1" : "Value 1",
   "type_2" : "Value 2",
   "type_3" : "Value 3",
   "type_4" : "Value 4",
}

How can I filter the value of type_2, which the key is created dynamically. Some thing like follow,

var number = 2; var value = config.type_+number;

Eranga Kapukotuwa
  • 4,542
  • 5
  • 25
  • 30

2 Answers2

3

In your case, you cannot use dot notation, that is why you should use "bracket notation" as it is for dynamic keys.

So, you can do it as:

var pre_str = "type_";
var number = 2;

var value = config[ pre_str + number ];
Community
  • 1
  • 1
yugantar kumar
  • 1,982
  • 1
  • 23
  • 33
2

Use bracket notation for dynamic keys

In your specific case

var value = config[ "type_" + number ];
gurvinder372
  • 66,980
  • 10
  • 72
  • 94