2013-08-04 1 views
0

Мне нужно вставить (json) объекты в массив, используемый на странице. Я использую nodejs с jade и предполагаю, что код ниже может соответствовать моей цели, но он не работает.Итерация внутри скрипта страницы с jade

//this code is in the template: 
    script. 
     otherPlayers = {}; 
     each player in playerList 
      otherPlayers["#{player.playerId}"] = !{JSON.stringify(#{player})}; 

На странице ожидаемый результат:

<script> 
    otherPlayers = {}; 
    otherPlayers[0] = {"playerId": 0, "playerName": "Leo" }; 
    otherPlayers[1] = {"playerId": 1, "playerName": "Daniel" }; 
    otherPlayers[2] = {"playerId": 2, "playerName": "Lucas" }; 
</script> 

Любой намек хорошо принят. Спасибо заранее.

ответ

2

Вы можете сделать, как показано ниже, в файле шаблона.

//In the template file 
script. 
    otherPlayers = {}; 
    var cplayerList = !{JSON.stringify(playerList)}; 
    for(player in cplayerList) { 
     otherPlayers[cplayerList[player].playerId] = cplayerList[player]; 
    } 
    console.log(otherPlayers); 
+0

Спасибо, что работает – NodeJsBeginner