数字未添加到LocalStorage

时间:2017-01-12 09:00:17

标签: javascript

我想在LocalStorage中为预先存在的数量添加数字。我发现之后确实确认有一个新的数量,但当我刷新页面时它不会保留到LocalStorage ......



//Just making an index
localStorage.setItem("currentUser",10)

//This makes currentUser.whatever into something viable
var currentUser = (JSON.parse(localStorage.getItem('usersArray')))[localStorage.getItem("currentUser")];

//I add the number to the pre-existing amount
currentUser.goldAmount = currentUser.goldAmount + 50;

//For reference 100 is a pre-existing amount for goldAmount, so it should now be 120. It does alert that although when i refresh the page and do it again its not 200, but still 150. It resets back to 100 every restart...
  alert(currentUser.goldAmount);




欣赏帮助:)

1 个答案:

答案 0 :(得分:-1)

尝试检查JSON和currentUser对象中的内容。 请注意,如果JSON包含 {"currentUser":{"goldAmount":10}} 比你需要阅读它 currentUser.currentUser.goldAmount variable in-json var requested var