我需要更改json文件中的state
,
我必须在我的json中使用HTML和javascript进行游戏,我的玩家拥有名称,密码和状态等属性。
我的请求只需要查看state
个数字,并在必要时进行更改。
如何编写仅更改player.state
的代码,从1到3?
这是JSON文件
[
{"pseudo":"player1","password":"player","state":"1"},
{"pseudo":"player2","password":"player","state":"0"}
]
答案 0 :(得分:0)
您可以执行以下操作:
function changeState(playersArray,playerToChange,newState){
playersArray.find(function(player) {return player.pseudo == playerToChange}).state = newState;
}
var players = [
{"pseudo":"player1","password":"player","state":"1"},
{"pseudo":"player2","password":"player","state":"0"}
]
changeState(players,"player2",3);