我正在開發一款遊戲應用程序。
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
請爲我提供解決方案。
的localStorage持有字符串。 – epascarello
是的,但我想知道如何將卡添加到loacl存儲當誰贏得 – user3433253
採取的對象,通過將其推到數組的末尾,並使用JSON.stringify將其重新存入存儲中添加新卡。 – epascarello