JavaScript:为循环动态创建变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6645067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:36:32  来源:igfitidea点击:

JavaScript: Dynamically Creating Variables for Loops

javascript

提问by user763349

How can I use a for loop to dynamically create variables, and be returned.

如何使用 for 循环动态创建变量并返回。

function createVariables()
{
for ( i=0; i<=20; i++ )
    {
        var account = i;
        return var account + i;
    }
 }

The goal is to have the result below:

目标是获得以下结果:

var account1;
var account2;
var account3; and etc.....

回答by Domenic

You should use an array:

你应该使用一个数组:

function createVariables(){
  var accounts = [];

  for (var i = 0; i <= 20; ++i) {
      accounts[i] = "whatever";
  }

  return accounts;
}

You then have access to accounts[0]through accounts[20].

然后您可以访问accounts[0]through accounts[20]

回答by NotMe

The only way I know how to do this would be to use the JavaScript evalfunction.

我知道如何做到这一点的唯一方法是使用 JavaScripteval函数。

Something like eval("account" + 1 + "='some value'");

就像是 eval("account" + 1 + "='some value'");

http://www.w3schools.com/jsref/jsref_eval.asp

http://www.w3schools.com/jsref/jsref_eval.asp

However, I think @Domenic has a better answer.

但是,我认为@Domenic 有更好的答案。

回答by whatevermike

I was unsure about answering an old questionhowever I stumbled across this while seeking an answer myself.

我不确定是否要回答一个老问题,但是我在自己寻求答案时偶然发现了这个问题。

for (var i = 1; i < 11; i++) { // Creating 10 objects
window["Object"+i] = new Object();
}
console.log(Object7); // is not undefined

The above code loops to 10 while creating dynamic objects, as described on https://www.codecademy.com/en/forum_questions/51068e93f73ad4947a005629

上述代码在创建动态对象时循环到 10,如https://www.codecademy.com/en/forum_questions/51068e93f73ad4947a005629 中所述

回答by Md Junaid Alam

You can use the eval() method to declare dynamic variables as it executes JavaScript statements passed to it.

您可以使用 eval() 方法在执行传递给它的 JavaScript 语句时声明动态变量。

function createVariables()
{
    for ( i=0; i<=20; i++ )
    {
        var str ="account"+ i+" = undefined";
        //Declaring and Setting dynamic variable to undefined using eval
        eval(str);
    }
}
createVariables();

回答by Jasp402

let etc = { name: 'foobar', city: 'xyz', company: 'companyName' };

Object.keys(etc).forEach(key=>{
window[`${key.toUpperCase()}`] = new Object(`${etc[`${key}`]}`)
});

console.log("-->"+NAME) //foobar

this is similar to what @whatevermikedescribes but it does not work in NodeJS because it uses window. :(

这类似于@whatevermike描述的内容,但它在 NodeJS 中不起作用,因为它使用 window. :(

回答by Cihan ba?

function createVariables() {
    var accounts = [];
    for (var i = 0; i <= 20; ++i) {
        accounts[i] = "merhaba" + i;
    }
    return accounts;
}