I have a hangman game that works with localStorage to save players' names and their scores, and there is a function that when the player wins eliminates the JSON object and then reinsertes it with the updated score:
comentarios = function () {
mostrarVidas.innerHTML = "Te quedan " + vidas + " oportunidades";
if (vidas < 1) {
mostrarVidas.innerHTML = "Partida Terminada";
}
for (var i = 0; i < adivinadas.length; i++) {
if (contador + espacio === adivinadas.length) {
mostrarVidas.innerHTML = "Ganaste!";
var index = -1;
plc = [];
for(var x in Jugadores){
var pl = JSON.parse(Jugadores[x]);
plc.push(pl);
}
var filteredObj = plc.find(function(item, i){
if(item.player === player){
index = i;
return i;
}
});
plc.splice(index, 1);
var jugador_push = JSON.stringify({
"player" : player,
"score" : score + 1
});
Jugadores.push(jugador_push);
localStorage.setItem("Jugadores", JSON.stringify(Jugadores));
}
}
};
But when the function is executed I duplicate the records in the JSON according to the number of letters of the word to guess. Then separate it into another function:
uptade_marcador = function () {
var index = -1;
plc = [];
for(var x in Jugadores){
var pl = JSON.parse(Jugadores[x]);
plc.push(pl);
}
var filteredObj = plc.find(function(item, i){
if(item.player === player){
index = i;
return i;
}
});
plc.splice(index, 1);
var jugador_push = JSON.stringify({
"player" : player,
"score" : score + 1
});
Jugadores.push(jugador_push);
localStorage.setItem("Jugadores", JSON.stringify(Jugadores));
};
But I do not know in which part of the code to insert it so that the records are not duplicated
Here is the complete code of the game . Thank you in advance