使用数组元素值声明多个变量名(Declare multiple variable names by using array element values)

编程入门 行业动态 更新时间:2024-10-28 01:19:16
使用数组元素值声明多个变量名(Declare multiple variable names by using array element values)

我想使用数组为变量创建唯一的名称。 我不打算在数组中存储任何计算值,而是能够使用数组声明变量来存储值。 我的尝试和研究如何实现这一点使我认为它甚至不可能。 如果有人可以让我知道,如果有可能的答案/示例,我会很感激。 我将发布一个简单的例子,说明我希望能够发挥作用。

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

本文发布于:2023-08-06 04:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数组   元素   声明   变量名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!