简体   繁体   English

自动在javascript数组中生成名称

[英]Automatically generate names in a javascript array

I have been struggling a lot with a piece of javascript code recently. 最近,我一直在努力用一段JavaScript代码。 The code looks like this: 代码如下:

var bigData = {
 "teams" : [
  ["Team 1",  "Team 2" ],
  ["Team 3",  "Team 4" ],
  ["Team 5",  "Team 6" ],
  ["Team 7",  "Team 8" ],
  ["Team 9",  "Team 10" ],
  ["Team 11",  "Team 12" ],
  ["Team 13",  "Team 14" ],
  ["Team 15",  "Team 16" ],
 ],
 results : [[ /* WINNER BRACKET */
  [[1,0], [1,0], [0,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
  [[1,2], [3,4], [5,6], [7,8]],
  [[9,1], [8,2]],
  [[1,3]]
 ]
}

As you might have guessed, it's a jquery plugin for tournaments. 您可能已经猜到了,它是锦标赛的jquery插件。 The problem is that I don't want to write the teams manually, I want them to be written automatically I have done this, and the code doesn't work, because the while loop is inside the variable (so far I know) : 问题是我不想手动编写团队,而是希望我自动编写团队,但代码却行不通,因为while循环位于变量内(到目前为止我知道):

var count = 1;
var bigData = {
 "teams" : [
  while (count <= 8) {
   ["Team ".count,  "Team ".count ],
   count++;
  }

 ],
 results : [[ /* WINNER BRACKET */
  [[1,0], [1,0], [0,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
  [[1,2], [3,4], [5,6], [7,8]],
  [[9,1], [8,2]],
  [[1,3]]
 ]
}

It won't work, and I really don't know what to do. 它行不通,我真的不知道该怎么办。

var bigData = {
"teams" : [],
results : [[ /* WINNER BRACKET */
  [[1,0], [1,0], [0,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
  [[1,2], [3,4], [5,6], [7,8]],
  [[9,1], [8,2]],
  [[1,3]]
]] };

for( var i=1 ; i<16 ; i+=2 )
    bigData.teams.push(['Team '+i,'Team '+(i+1)]);

console.log(JSON.stringify(bigData));

In console: 在控制台中:

{"teams":[["Team 1","Team 2"],["Team 3","Team 4"],["Team 5","Team 6"],["Team 7","Team 8"],["Team 9","Team 10"],["Team 11","Team 12"],["Team 13","Team 14"],["Team 15","Team 16"]],"results":[[[[1,0],[1,0],[0,3],[2,3],[1,5],[5,3],[7,2],[1,2]],[[1,2],[3,4],[5,6],[7,8]],[[9,1],[8,2]],[[1,3]]]]} {“ teams”:[[“ Team 1”,“ Team 2”],[“ Team 3”,“ Team 4”],[“ Team 5”,“ Team 6”],[“ Team 7”,“ Team 8“],[” Team 9“,” Team 10“],[” Team 11“,” Team 12“],[” Team 13“,” Team 14“],[” Team 15“,” Team 16“ ]],“结果”:[[[[1,0],[1,0],[0,3],[2,3],[1,5],[5,3],[7,2 ],[1,2]],[[1,2],[3,4],[5,6],[7,8]],[[9,1],[8,2]],[ [1,3]]]]}

Try this: 尝试这个:

var count = 1;
var data = [];

while (count <= 16) {
  data.push(["Team " + (count++).toString(),  "Team " + count.toString() ]);
  count++;
 }

var bigData = {
"teams" : data
}

It builds the array by pushing the teams into it. 它通过将团队推入其中来构建阵列。 Note the concatenation of count as a string to the team name, plus the ++ on the first count to iterate it to the next team (thus producing Team 1 and Team 2, not Team 1 and Team 1). 请注意,将计数作为字符串连接到团队名称,再加上第一个计数上的++以将其迭代到下一个团队(因此产生了Team 1和Team 2,而不是Team 1和Team 1)。

and the code doesnt work, because the while loop is inside the variable 代码不起作用,因为while循环位于变量内部

Yes, you cannot put control flow statements in the midst of an object literal. 是的,您不能将控制流语句放在对象文字中间。 Instead, you will need to start with an empty array, and then fill that with values via assignments (or push ): 相反,您需要从一个空数组开始,然后通过赋值(或push )用值填充:

var teams = [];
for (var i=1; i<=16; )
    teams.push(['Team '+i++,'Team '+i++]);

var bigData = {
    teams: teams,
    results: …
};

The other answers provided here build a list of teams using a set number (8, 16, whatever). 此处提供的其他答案将使用设定的数字(8、16,无论如何)构建球队列表。

You can get a little cheeky and generate the list of teams off the teams in the actual results instead, which will future-proof your script against your league size expanding. 您可能会显得有些厚脸皮,而是在实际结果中从球队中得出球队名单,这将使您的脚本适应未来的联赛规模不断扩大。 Code spends most of its lifetime in maintenance, if you can spend just a few extra minutes to make your stuff more data-driven, you'll save plenty of time over the long haul. 代码将其一生的大部分时间都花在维护上,如果您可以花几分钟额外的时间使您的内容变得更加由数据驱动,那么从长远来看,您将节省大量时间。

Since you're using jquery, I'll make some use of the functions in that library here: 由于您使用的是jquery,因此我将在这里使用该库中的函数:

var bigData = {
  results : [
    [ /* WINNER BRACKET */
      [[1,0], [1,0], [0,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
      [[1,2], [3,4], [5,6], [7,8]],
      [[9,1], [8,2]],
      [[1,3]]
    ]
  ]
};

function flatten(n) {
  return $.map(n, function(x){ 
    return $.isArray(x) ? flatten(x) : x;
  });
}

bigData.teams = $.map(
  $.unique(flatten(bigData.results)).sort(), 
  function(num) { return "Team "+num; }
);

Console output: 控制台输出:

> JSON.stringify(bigData)
  "{
    "results":[[[[1,0],[1,0],[0,3],[2,3],[1,5],[5,3],[7,2],[1,2]],[[1,2],[3,4],[5,6],[7,8]],[[9,1],[8,2]],[[1,3]]]],
    "teams":["Team 0","Team 1","Team 2","Team 2","Team 2","Team 3","Team 4","Team 5","Team 6","Team 7","Team 8","Team 8","Team 9"]
   }"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM