I am saving on the local storage this item on the key perfil.
[{"inscricao":{"nome":"Duarte","passe":"1234","email":"abcd@abcd.com"},"estatisticas":{"totalJogos":0,"totalTempo":0}}]
My question is how do I change the field totalJogos to 1 for example. I tried doing this...
function get_localstorage(){
let x = localStorage.getItem("perfil")
return JSON.parse(x)
}
get_localstorage()[0]["estatisticas"]["totalJogos"] = 1;
...but it doesn't change anything. How can I change it ?
You need to set it again after changing it:
function get_localstorage(){
let x = localStorage.getItem("perfil")
return JSON.parse(x)
}
const newValue = get_localstorage();
newValue[0]["estatisticas"]["totalJogos"] = 1;
localStorage.setItem("perfil", JSON.stringify(newValue));