2017-01-12 2 views
-2

LocalStorageの既存の金額に数値を加算します。私は後に見つけること...確かに新しい量があることが認識されているが、私はページを更新するとき、それはの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);

助けに感謝です。 )

+0

ようにそれを読む必要がない数という 注意? – JJJ

+2

再度値を変更した後は、localstorageに設定するか、更新しないでください。 – Manish

+0

更新された値をlocalStorageに保存する必要があります。また、すべてがローカルストアの "文字列"として保存されることに注意してください。正しい数学演算のために値を解析する必要があります。 –

答えて

-1

あなたのJSONとcurrentUserオブジェクトの中のwhatsを調べてみてください。 JSONは {"currentUser":{"goldAmount":10}} が含まれている場合、あなたが戻って新しい金額を節約するコードがのlocalStorageするのWHERE currentUser.currentUser.goldAmount variablein-json varrequested var

関連する問題