Duarte GV
Duarte GV

Reputation: 17

How to change a value in local storage

I am saving on the local storage this item on the key perfil.

[{"inscricao":{"nome":"Duarte","passe":"1234","email":"[email protected]"},"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 ?

Upvotes: 0

Views: 596

Answers (1)

noiseymur
noiseymur

Reputation: 876

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));

Upvotes: 2

Related Questions