I have JS objects as below.
var round=[
{
"round":"1",
"score":"8",
"players":[
{
"id":"28259",
"name":"Player 1"
}
]
},
{
"round":"2",
"score":"0",
"players":[
{
"id":"",
"name":"Player 2"
},
{
"id":"1887",
"name":"Player 3"
}
]
},
{
"round":"3",
"score":"0",
"players":[
{
"id":"26300",
"name":"Player 4"
}
]
},
{
"round":"4",
"score":"5",
"players":[
{
"id":"22792",
"name":"Player 5"
},
{
"id":"23135",
"name":"Player 8"
},
{
"id":"28259",
"name":"Player 9"
}
]
}
]
I want to create a row per a player with round and score data. However, I can only create one row per a round. Players are displayed in the same cell. How can I make a separate row per a player?
What I want to display:
- 1 Player 1 8
- 2 Player 2 0
- 2 Player 3 0
- 3 Player 4 0
- 4 Player 5 5
- 4 Player 6 5
- 4 Player 7 5
What I am getting now:
- 1 Player 1 8
- 2 Player 2 Player 3 0
- 3 Player 4 0
- 4 Player 5 Player 6 Player 7 5
Here is JS Fiddle - http://jsfiddle.net/sunflowersh/8gm96vts/2/
JavaScript:
function write_round_players(){
$('#tableData').html('');
$.each(round, function(index,value){
var round_row = '';
round_row+='<div class="playerRow">';
round_row+=' <div class="round">'+this.round+'</div>';
round_row+=' <div class="player">';
var round_players = this.players;
$.each(round_players, function(index,value){
if(this.id != ""){
round_row+='<a href="/player_'+this.id+'.html">'+this.name+'</a>';
} else {
round_row+=this.name;
}
});
round_row+=' </div>';
round_row+=' <div class="score">'+this.score+'</div>';
round_row+='</div>';
$('#tableData').append(round_row);
});
}