JavaScript中的变量对象声明名称

时间:2016-08-01 16:55:02

标签: javascript arrays object

希望创建一组数组大小的对象。在循环中我想要定义这些对象:

var playersNames = ["name1", "name2", "name3"];

for(i=0; i < playersNames.length; i++){
    var player[i] = new player();
    player[i].name = playersNames[i];
}

但JavaScript并不像var player[i]

我能做到这一点吗?

4 个答案:

答案 0 :(得分:1)

首先创建一个数组,然后 export PYTHONDONTWRITEBYTECODE= 创建一个数组:

push()

答案 1 :(得分:1)

您可以预先分配播放器阵列:

    Word.Application word = new Word.Application();
    word.Visible = true;
    Word.Document d1 = word.Documents.Add();
    Word.Document d2 = word.Documents.Open(@"E:\00-Word\Test.docx");
    Word.Range oRange = d2.Content;
    oRange.Copy();
d1.Content.PasteSpecial(DataType:Word.WdPasteOptions.wdKeepSourceFormatting);

答案 2 :(得分:1)

或者你可以轻松地做这样的事情 -

to_field

答案 3 :(得分:0)

使用map

var players = playersNames.map(playerName => {
  var myPlayer = new player();
  myPlayer.name = playerName;
  return myPlayer;
});

这样可以避免必须预先解析您的players数组,并逐步推进它。从本质上讲,map可以帮助您。

如果您重新设计构造函数以将名称作为参数,则它更紧凑:

var players = playerNames.map(playerName => new player(playerName));