从本地存储中的对象删除数组 [英] Delete array from object in localstorage

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

问题描述

当我单击按钮时,将加载带有参数"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);给我这个输出:

{"21114":{"id":"21114","external_id":","sessiongroupid":"1844","eventid":"5588","order":"0",名称:""localStorage HTML5会话",描述":"localstorage",开始时间":"2013-04-23 12:00:00",结束时间":"2013-04-23 13:30:00", "speaker":","location":","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl": ","presentation":","organizer":"0","twitter":","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url:"," venueid:" 0}, "21115:{" id:" 21115," external_id:"," sessiongroupid:" 1845," eventid:" 5588," order:" 0," name:" tweede",说明":"tweede",开始时间":"2013-04-03 00:00:00",结束时间":"2013-04-04 00:00:00",扬声器":" "location":","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":","presentation": ","organizer":"0","twitter":","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":",会场标识号:"0"},"21118":{"id":"21118","external_id":","sessiongroupid":"1848","eventid":"5588","order":"0 ,"名称:" javascript会话,"描述:"关于javascript的会话,"开始时间:" 2013-05-15 12:00:00,"结束时间:" 2013-05-15 12: 30:00," speaker:"," location:" waregem," mapid:" 0," xpos:" 0.000000," ypos:" 0.000000," maptype:" plan ," imageurl:"," presentation:"," organizer:" 0," twitter:"," allowAddToFavorites:" 0," allowAddToAgenda:" 0," votes":"0","url":","venueid":"0"}}

{"21114":{"id":"21114","external_id":"","sessiongroupid":"1844","eventid":"5588","order":"0","name":"localStorage HTML5 Session","description":"localstorage","starttime":"2013-04-23 12:00:00","endtime":"2013-04-23 13:30:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"}, "21115 :{"id":"21115","external_id":"","sessiongroupid":"1845","eventid":"5588","order":"0","name":"tweede","description":"tweede","starttime":"2013-04-03 00:00:00","endtime":"2013-04-04 00:00:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"},"21118":{"id":"21118","external_id":"","sessiongroupid":"1848","eventid":"5588","order":"0","name":"javascript session","description":"session about javascript","starttime":"2013-05-15 12:00:00","endtime":"2013-05-15 12:30:00","speaker":"","location":"waregem","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"}}

如您所见,我有一个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".

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆