admin管理员组文章数量:1402274
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.
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.
Share Improve this question asked Nov 12, 2012 at 7:57 Santosh SSantosh S 4,3556 gold badges35 silver badges37 bronze badges1 Answer
Reset to default 8Use 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.
本文标签: javascript concat string to objectStack Overflow
版权声明:本文标题:javascript concat string to object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744352028a2602108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论