从本地存储中的对象删除数组

从本地存储中的对象删除数组

本文介绍了从本地存储中的对象删除数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,将加载带有参数"id"的函数"DeleteFromlocalStorage".这是我的函数"DeleteFromlocalStorage":

When I click on a button I load the function "DeleteFromlocalStorage" with the parameter "id".This is my function "DeleteFromlocalStorage":

        function DeleteSessionFromLocalStorage(data)
    {
        var id_session = data;

        a = localStorage.getItem('session');

        alert(a);

    }

我的alert(a);给我这个输出:

如您所见,我有一个json字符串.密钥始终是ID.现在我要删除id =参数id的json.

As you can see I have a json string. The key is always the id. Now I want to delete the json with the id = parameter id.

我将必须获取对象并删除子对象,然后在我的localStorage中恢复该对象.有人知道我该怎么做吗?

I will have to get the object and delete the subobject and restore the object in my localStorage. Does anybody know how I can do this?

提前谢谢!

推荐答案

for(obj in json) {
            if(json[obj].id == id_session)
            {
                delete json[obj];

            }
        }

        localStorage.setItem('session', JSON.stringify(json));

获取对象,检查每个对象的每个ID,然后删除ID ="id_session"的对象.

Get the objects, check every id of every object and delete the ones with the id = "id_session".

这篇关于从本地存储中的对象删除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:22