本地存储和JSON:由于 localstorage.removeItem 需要整个密钥,如何仅删除密钥内的 1 个数组
我的 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"}]
当 localstorage.removeItem
需要整个密钥时,如何仅删除 id:item-3
?
我使用此方法来更新数组中的特定值: http://jsfiddle.net/Qmm9g/ 所以使用相同的方法我想删除特定的数组。
请注意,已经有一个删除按钮。我想要一个可以删除整个数组的按钮 ({"id":"item-3","href":"google.com","icon":"google.com"}
) 与 ID:item-3
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"}]
How can I delete only the id:item-3
when localstorage.removeItem
requires entire key?
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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 -
您可以将 jQuery 的 $.each() 函数与 JavaScript 的 splice 方法删除整个对象,如下所示:
更新的小提琴:http://jsfiddle.net/Qmm9g/3/
我希望这有帮助!
You can use jQuery's $.each() function along with JavaScript's splice method to remove the entire object like this:
Updated Fiddle: http://jsfiddle.net/Qmm9g/3/
I hope this helps!
这是我从 localStorage 中删除对象的代码。
This is my code to delete object from localStorage.