0

当我单击一个按钮时,我使用参数“id”加载函数“DeleteFromlocalStorage”。这是我的函数“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","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":"关于 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":"","演示文稿":"","组织者":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":" ","venueid":"0"}}

如您所见,我有一个 json 字符串。关键始终是 id。现在我想删除带有 id = 参数 id 的 json。

我将不得不获取对象并删除子对象并在我的 localStorage 中恢复对象。有人知道我该怎么做吗?

提前致谢!

4

3 回答 3

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

            }
        }

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

获取对象,检查每个对象的每个 id 并删除 id = "id_session" 的对象。

于 2013-04-23T07:41:55.963 回答
0

您的代码示例对我来说就像一个 javascript 对象。在这种情况下,您可以使用“删除”关键字。这里很好的讨论:

如何从 JavaScript 对象中删除属性?

所以像

删除 ["21114"]

于 2013-04-23T07:10:50.870 回答
0

我可能错了,但这是你想要做的:

任何一个localStorage.removeItem(key);

或者,如果在 localStorage JSON 项中存在某些内容,则执行以下操作:

  function DeleteSessionFromLocalStorage(data)
{
    var id_session = data;
    //Not sure but you might need to do JSON.parse(a) after to get it
    a = localStorage.getItem('session');

    delete a.data
    alert(a);

}
于 2013-04-23T07:07:39.260 回答