我想使用数组为变量创建唯一的名称。 我不打算在数组中存储任何计算值,而是能够使用数组声明变量来存储值。 我的尝试和研究如何实现这一点使我认为它甚至不可能。 如果有人可以让我知道,如果有可能的答案/示例,我会很感激。 我将发布一个简单的例子,说明我希望能够发挥作用。
var indices = ["index01", "index02", "index03"]; var keys = ["key01", "key02", "key03"]; for (var index = 0; index < indices.length; index++) { for (var key = 0; key < keys.length; key++) { var indices[index]+keys[key] //Looking for var index01key01, var index01key02 etc... } }I’m looking to use arrays to create unique names for variables. I am not looking to store any calculated values in the arrays, but rather be able to declare variables using arrays to store the values. My attempts and research how to accomplish this leads me to think that it’s not even possible. I’d appreciate it if someone could let me know and if it is possible an answer/example on how to do it. I’ll post a simplified example on what I’m hoping to get working.
var indices = ["index01", "index02", "index03"]; var keys = ["key01", "key02", "key03"]; for (var index = 0; index < indices.length; index++) { for (var key = 0; key < keys.length; key++) { var indices[index]+keys[key] //Looking for var index01key01, var index01key02 etc... } }最满意答案
好吧,基本的javascript变量都在window范围内,所以试试:
var indices = ["index01", "index02", "index03"]; var keys = ["key01", "key02", "key03"]; for (var index = 0; index < indices.length; index++) { for (var key = 0; key < keys.length; key++) { // you can now use the variable as window.index01key01, or just index01key01 window[indices[index]+keys[key]] = null; } }Well, basic javascript variables are in the window scope, so try:
var indices = ["index01", "index02", "index03"]; var keys = ["key01", "key02", "key03"]; for (var index = 0; index < indices.length; index++) { for (var key = 0; key < keys.length; key++) { // you can now use the variable as window.index01key01, or just index01key01 window[indices[index]+keys[key]] = null; } }更多推荐
能够,arrays,var,index,电脑培训,计算机培训,IT培训"/> <meta name="descripti
发布评论