jQuerybeast jQuerybeast - 2 years ago 122
Javascript Question

Localstorage & JSON: How can I delete only 1 array inside a key since localstorage.removeItem requires the entire key

I have this in my localStorage:


How can I delete only the
requires entire key?

I use this method to update a specific value in an array: http://jsfiddle.net/Qmm9g/ so using the same method I want to delete specific array.

Note that there is already a button to delete. That button I want a function which will delete the entire array (
) with

Answer Source

Something like this would work, I'm not sure if it's the best way to do it though. There maybe a better local storage specific way -

var json = JSON.parse(localStorage["results"]);
for (i=0;i<json.length;i++)
            if (json[i].id == 'item-3') json.splice(i,1);
localStorage["results"] = JSON.stringify(json);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download