i need to insert (json) objects into an array used in a page. I'm using nodejs with jade and suppose that the code below might fit my purpose, but it doesnt work.
//this code is in the template:
script.
otherPlayers = {};
each player in playerList
otherPlayers["#{player.playerId}"] = !{JSON.stringify(#{player})};
In the page the expected result is:
<script>
otherPlayers = {};
otherPlayers[0] = {"playerId": 0, "playerName": "Leo" };
otherPlayers[1] = {"playerId": 1, "playerName": "Daniel" };
otherPlayers[2] = {"playerId": 2, "playerName": "Lucas" };
</script>
Any hint is well accepted. Thanks in advance at all.
You can do as below in your template file.
//In the template file
script.
otherPlayers = {};
var cplayerList = !{JSON.stringify(playerList)};
for(player in cplayerList) {
otherPlayers[cplayerList[player].playerId] = cplayerList[player];
}
console.log(otherPlayers);