本地存储和JSON:由于 localstorage.removeItem 需要整个密钥,如何仅删除密钥内的 1 个数组

发布于 2024-12-15 05:34:32 字数 869 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-12-22 05:34:32

像这样的东西会起作用,但我不确定这是否是最好的方法。也许有更好的本地存储特定方式 -

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);

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);
那片花海 2024-12-22 05:34:32

您可以将 jQuery 的 $.each() 函数与 JavaScript 的 splice 方法删除整个对象,如下所示:

$.each(json, function(index, obj){
    if (obj.id == 'item-3') {
        json.splice(index,1);
        console.log(json);
        localStorage["results"] = JSON.stringify(json);
        return false;
    }
});

更新的小提琴: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:

$.each(json, function(index, obj){
    if (obj.id == 'item-3') {
        json.splice(index,1);
        console.log(json);
        localStorage["results"] = JSON.stringify(json);
        return false;
    }
});

Updated Fiddle: http://jsfiddle.net/Qmm9g/3/

I hope this helps!

长亭外,古道边 2024-12-22 05:34:32

这是我从 localStorage 中删除对象的代码。

 {
"admin": {
    "pass": "1234",
    "task": [
        {"id": "1", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "2", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "3", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "4", "taskName": "assignedTask", "taskDesc": "jhdjshdh"}
    ]
 }


    function filterData() {
        var data = JSON.parse(localStorage.task);
        //console.log(data);
        var newData = data.filter(function(val){
            return (val.YourPropertyName !== key.value && val.YourPropertyName !== val.value );
        });
        localStorage.task = JSON.stringify(newData);
    }

This is my code to delete object from localStorage.

 {
"admin": {
    "pass": "1234",
    "task": [
        {"id": "1", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "2", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "3", "taskName": "assignedTask", "taskDesc": "jhdjshdh"},
        {"id": "4", "taskName": "assignedTask", "taskDesc": "jhdjshdh"}
    ]
 }


    function filterData() {
        var data = JSON.parse(localStorage.task);
        //console.log(data);
        var newData = data.filter(function(val){
            return (val.YourPropertyName !== key.value && val.YourPropertyName !== val.value );
        });
        localStorage.task = JSON.stringify(newData);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文