1

I have been struggling a lot with a piece of javascript code recently. 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. 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) :

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.

4 Answers 4

4
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]]]]}

Sign up to request clarification or add additional context in comments.

3 Comments

You guys are the best really, i can't sayt how much you have done for me! - TY SO much to all of you, have a nice day :)
Okay sir! - Now i have another question for you.. Is there anyway that i can pass in php code to the team names?
@Emil I'm sure there is a way - if you post another question I'm sure you'll get some answers.
0

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).

2 Comments

it will add only 8 teams, not 16 :)
also, using toString() is not necessary
0

and the code doesnt work, because the while loop is inside the variable

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):

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

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

Comments

0

The other answers provided here build a list of teams using a set number (8, 16, whatever).

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:

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"]
   }"

2 Comments

Hey there. Thanks for your apply. I understand what you mean, but not what you are typing :p - Am i suppose to copy the code, and it will display (because i can't) - i rly don't know what to do with it :)
The first block should just be a drop in replacement of the code you listed, like the other answers provided here. The second block is example output from putting this in to a javascript console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.