问题描述
我有这个在我的localStorage:
I have this in my 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:项目3时
localstorage.removeItem
需要整个密钥
How can I delete only the id:item-3
when localstorage.removeItem
requires entire key?
我用这个方法来更新数组中的一个特定的值: http://jsfiddle.net/Qmm9g/一>因此,使用我想删除特定阵列的方法相同。
I use this method to update a specific value in an array: http://jsfiddle.net/Qmm9g/ so using the same method I want to delete specific array.
请注意,已经有一个按钮来删除。该按钮我想这将删除整个阵列(函数{ID:项-3,HREF:google.com,图标:google.com}
)使用 ID:项目-3
Note that there is already a button to delete. That button I want a function which will delete the entire array ({"id":"item-3","href":"google.com","icon":"google.com"}
) with ID:item-3
推荐答案
这样的事情会的工作,我不知道,如果它是的最的方式做到这一点,虽然。也许有一个更好的本地存储特殊的方式 -
Something like this would work, I'm not sure if it's the best way to do it though. There maybe a better local storage specific way -
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);
这篇关于localStorage的&安培; JSON:我如何删除只有1关键内部阵列,因为localstorage.removeItem需要整个关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!