我的localStorage中有这个:
[{"id":"item-1","href":"google.com","icon":"google.com"}, {"id":"item-2","href":"youtube.com","icon":"youtube.com"}, {"id":"item-3","href":"google.com","icon":"google.com"}, {"id":"item-4","href":"google.com","icon":"google.com"}, {"id":"item-5","href":"youtube.com","icon":"youtube.com"}, {"id":"item-6","href":"asos.com","icon":"asos.com"}, {"id":"item-7","href":"google.com","icon":"google.com"}, {"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}]
如何只删除id:item-3何时localstorage.removeItem需要整个密钥?
id:item-3
localstorage.removeItem
我使用此方法来更新数组中的特定值:http : //jsfiddle.net/Qmm9g/,因此使用与我想删除特定数组相同的方法。
请注意,已经有一个要删除的按钮。该按钮我想要一个函数,该函数将删除整个数组({"id":"item-3","href":"google.com","icon":"google.com"})ID:item-3
{"id":"item-3","href":"google.com","icon":"google.com"}
ID:item-3
像这样的事情会起作用,但是我不确定这是否是 最好的 方法。有一种更好的本地存储特定方式-
var json = JSON.parse(localStorage["results"]); for (i=0;i<json.length;i++) if (json[i].id == 'item-3') json.splice(i,1); localStorage["results"] = JSON.stringify(json);