2014-05-16 68 views
-1

我正在開發一款遊戲應用程序。
Internally我做了兩個loaclstorage,一個是userCard和另一個CompCard,我向本地存儲添加了3個板球選手卡細節。如何將數據添加到本地存儲?

我的2個本地存儲和deatils:

var user_Card = [["Nixon", "McLean", "West Indies", 
        45, 314, 0, 1, "12.07", "37.58", 
        46, 3, 21, 8, 
        "img/cards/7RBKWQPJAG_NixonMcLean.jpg", 1], 
        ["Brian", "McMillan", "South Africa", 78, 
        841, 1, 0, "23.36", "36.98", 70, 4, 32, 43, 
        "img/cards/Y9U5UKA60O_BrianMcMillan.jpg", 2], 
        ["Craig", "McMillan", "New Zealand", 197, 4707, 3, 
        28, "28.18", "35.04", 49, 3, 20, 44, 
        "img/cards/WE0NUNG80C_CraigMcMillan.jpg", 3] 
       ];    

var comp_Card = [["Geoff", "Miller", "England", 25, 136, 0, 0, 
        "8.5", "32.52", 25, 3, 27, 4, 
        "img/cards/7ZPIQXC19H_GeoffMiller.jpg", 4], 
        ["Kyle", "Mills", "New Zealand", 165, 1016, 
        0, 2, "15.87", "26.74", 235, 5, 25, 40, 
        "img/cards/P43DTA4ZCX_KyleMills.jpg", 5], 
        ["Minhazul", "Adedin", "Bangladesh", 27, 453, 0, 2, 
        "18.87", "39.3", 13, 2, 39, 2, 
        "img/cards/CC8ENY3E09_MinhazulAdedin.jpg", 6] 
        ]; 

userCard =JSON.parse(localStorage['user_Card']); 
compCard =JSON.parse(localStorage['comp_Card']); 

當我檢索所有卡的詳細信息來了,usercard[0]compcard [0]是第一張牌,usercard[1]是第二張牌,usercard[2]是第三張牌,

我的問題是當我玩遊戲時,如果用戶卡贏了,我想添加丟失的compcard到用戶卡本地storgae。如果compcard wins將失去的用戶卡添加到compcard本地存儲中。

假設userCard[0]是第一張牌 compcard[0]是第一張牌

當我玩遊戲,如果userCard[0]勝,我想補充失去compcard[0]到localStorage的userCard如果compCard[0]勝,我想補充失去usercard[0]本地存儲compCard

請爲我提供解決方案。

+0

的localStorage持有字符串。 – epascarello

+0

是的,但我想知道如何將卡添加到loacl存儲當誰贏得 – user3433253

+2

採取的對象,通過將其推到數組的末尾,並使用JSON.stringify將其重新存入存儲中添加新卡。 – epascarello

回答

1

你怎麼可以讀取Offline Technologies

網絡存儲只是提供鍵值映射,例如 localStorage [「name」] = username ;.不幸的是,目前的 實現僅支持字符串到字符串映射,所以您需要對其他數據結構進行串行化和反序列化。您可以使用 JSON.stringify()和JSON.parse()。

所以你應該嘗試這樣的事:

var userCard =JSON.parse(localStorage.['user_Card']); 
userCard.push(compCard[0]); 

localStorage.['user_Card'] = JSON.stringify(userCard)); 
相關問題