admin管理员组文章数量:1391782
i repeat the title because everything is there : How to pass a variable in array index
var xyz = 0;
var somearray = ['a','b','c'];
var content = somearray[xyz]; - **that dont work !**
what should be the RIGHT way to do that ?
i repeat the title because everything is there : How to pass a variable in array index
var xyz = 0;
var somearray = ['a','b','c'];
var content = somearray[xyz]; - **that dont work !**
what should be the RIGHT way to do that ?
Share Improve this question asked Jan 26, 2011 at 22:49 menardmammenardmam 9,98629 gold badges87 silver badges114 bronze badges 2- actually the above does work (at least in my test in firebug I get content = 'a'). What about the above isn't working for you? – scrappedcola Commented Jan 26, 2011 at 22:55
- works heres a demo – Aly Commented Jan 26, 2011 at 22:55
2 Answers
Reset to default 1Just a stab in the dark here, but perhaps the OP is using inArray and might be asking (indirectly) how to get the intellisense working in whatever tool they're using.
If that's the case, I'm sure someone here can provide a more elegant solution but something similar to the following should work:
var somearray = ['a','b','c'];
var index = $.inArray('a', somearray);
if (index > -1) {
index = isNaN(index) ? 0 : index;
var content = somearray[index];
}
That actually is correct. After executing your code, minus the ment, content contains 'a'.
<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript">
var xyz = 0;
var somearray = ['a','b','c'];
var content = somearray[xyz];
alert(content);
</script>
</body>
</html>
You should get a nice little alert box that says "a".
本文标签: javascriptHow to pass a variable in array indexStack Overflow
版权声明:本文标题:javascript - How to pass a variable in array index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744771765a2624382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论