0

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

    }

My alert(a); gives me this output:

{"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"}}

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.

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?

Thanks in advance!

5
  • 1
    Looks more like a JSON string to me ? Commented Apr 23, 2013 at 7:03
  • Where is the array you asked about? Commented Apr 23, 2013 at 7:06
  • If you know the key then use localStorage.removeItem(key). Commented Apr 23, 2013 at 7:06
  • yes, sorry. it's a json string Commented Apr 23, 2013 at 7:06
  • For a start you JSON is valid see jsonlint.com Commented Apr 23, 2013 at 7:19

3 Answers 3

0

I may be wrong but here is what you want to do:

Either localStorage.removeItem(key);

Or if ifs something within the localStorage JSON item then do:

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work. I will have to get the object and delete the subobject and restore the object in my localStorage. Do you know how I can do this?
0

Your code sample just looks like a javascript object to me. In which case you can use the 'delete' keyword. Good discussion here:

How do I remove a property from a JavaScript object?

So something like

delete a["21114"]

2 Comments

Doesn't work. I will have to get the object and delete the subobject and restore the object in my localStorage. Do you know how I can do this?
if 'localStorage.getItem('session');' is actually returning a string (which your question is not accurately showing) then you need to turn it into a javascript object. var obj = JSON.parse(localStorage.getItem('session')); delete obj["key"];
0
for(obj in json) {       
            if(json[obj].id == id_session)
            {
                delete json[obj];

            }
        }

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.