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
  • 3,722
  • 4
  • 22
  • 29

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,802
  • 1
  • 21
  • 31
2

Use bracket notation for dynamic keys

In your specific case

var value = config[ "type_" + number ];
gurvinder372
  • 64,240
  • 8
  • 67
  • 88