当我点击一个按钮时,我加载了带有参数“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”, “事件ID”: “5588”, “订单”: “0”,“名称“:”localStorage HTML5 Session“,”description“:”localstorage“,”starttime“:”2013-04-23 12:00:00“,”endtime“:”2013-04-23 13:30:00“, “扬声器”: “”, “位置”: “”, “的azazaz”: “0”, “XPOS”: “0.000000”, “ypos”: “0.000000”, “地图类型”: “计划”, “IMAGEURL”: “”, “演讲”: “”, “组织者”: “0”, “推特”: “”, “allowAddToFavorites”: “0”, “allowAddToAgenda”: “0”, “票”: “0”,”网址 “:””, “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”:“”, “位置”: “”, “的azazaz”: “0”, “XPOS”: “0.000000”, “ypos”: “0.000000”, “地图类型”: “计划”, “IMAGEURL”: “”, “演示”: “”, “组织者”: “0”, “推特”: “”, “allowAddToFavorites”: “0”, “allowAddToAgenda”: “0”, “票”: “0”, “URL”: “”,” venueid “:” 0 “},” 21118 “:{” ID “:” 21118" , “EXTERNAL_ID”: “”, “sessiongroupid”: “1848”, “事件ID”: “5588”, “订单”:“0 “,”name“:”javascript session“,”description“:”关于javascript的会话“,”starttime“:”2013-05-15 12:00:00“,”endtime“:”2013-05-15 12: 30:00" , “扬声器”: “”, “位置”: “瓦勒海姆”, “的azazaz”: “0”, “XPOS”: “0.000000”, “ypos”: “0.000000”, “地图类型”:“计划”, “IMAGEURL”: “”, “演讲”: “”, “组织者”: “0”, “推特”: “”, “allowAddToFavorites”: “0”, “allowAddToAgenda”: “0”, “投票” : “0”, “URL”: “”, “venueid”: “0”}}
如你所见,我有一个json字符串。关键始终是id。现在我想删除带有id =参数id的json。
我必须获取对象并删除子对象并恢复localStorage中的对象。有人知道我怎么做吗?
提前致谢!
答案 0 :(得分:1)
如果您需要编辑它,那么将任何大型字符串化为单个localStorage条目的效率非常低。
您需要http://rhaboo.org - 这就是它的用途。它是localStorage的包装器,它使用大量localStorage条目,因此您可以有效地编辑大对象的位。
API非常简单,例如:
var store = Rhaboo.persistent('Sessions etc');
//Initialise only if the store seems virginal...
if (store.sessions === undefined) store.write('sessions', {
"21114":{"id":"21114","external_id":"","sessiongroupid":"1844","eventid":"5588"},
"21115":{"id":"21115","external_id":"","sessiongroupid":"1845","eventid":"5588"},
"21118":{"id":"21118","external_id":"","sessiongroupid":"1848","eventid":"5588"}
});
console.log(store.sessions[21114].eventid);
store.sessions.erase(21115);
BTW,我写了rhaboo。
答案 1 :(得分:0)
我可能错了,但这就是你想做的事情:
localStorage.removeItem(key);
或者如果在localStorage JSON项目中有ifs,那么:
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);
}
答案 2 :(得分:0)
您的代码示例对我来说就像是一个javascript对象。在这种情况下,您可以使用'delete'关键字。这里讨论很好:
How do I remove a property from a JavaScript object?
类似
删除[“21114”]
答案 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”的对象。