简体   繁体   中英

javascript concat string to object

Please check code below. Here all variable values are static.

var o = { level_a:{}, level_b:{}, . . . .};

var levelVar = "b";

var selected_tab = 'level'+'_'+levelVar; \\\\level_b

var result = o.selected_tab;

Here you can see var o is object and var levelVar and selected_tab are string. Now I expect I should get value of o.level_b inside result , but its not working becuse we can not concat string to object.

Please help.

Use this notation :

result = o[selected_tab];

More generally, when you have var obj={a:'b'} , you can access the property a using both obj.a and obj['a'] .

Here's a MDN reference about the use of objects and properties .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM