Remove Json from localstorage

I don't know if I'm over complicating things.. but I'm trying to have an ability to remove an json object in local storage. There is only one localstorage item holding all the data.

I get no errors but when I go back to the page the item is still there.

Console Logged Json

{
"cities": [
    {
        "id": 0,
        "storename": "test",
        "notes": "test",
        "rejected": "on",
        "offer": "test"
    },
    {
        "id": 1,
        "storename": "test2",
        "notes": "test2",
        "rejected": "on",
        "offer": "test2"
    }
]

}

Jquery

$('.deleteButton').click(function(){

        var id = $(this).data('centreid');

        $(this).remove();

        localStorage.clear();

        console.log(JsonData);

        $.each($.parseJSON(JsonData), function(idx, cities) {

            $.each(cities, function(idx, obj){

                delete id[id];

            });     
        });

        var Count = 0;
        localStorage.setItem(Count, JsonData);  
        JsonData = localStorage.getItem(Count);

Answers


The problem is that your $.parseJSON(JsonData) returns parsed object, than you change this object, but didn't store anywhere, so it goes. You need to store parsed object to variable, modify and than call SetItem with this object. Working example:

$('.deleteButton').click(function(){
    var id = $(this).data('centreid');
    $(this).remove();
    localStorage.clear();
    console.log(JsonData);

    var data = JSON.parse(JsonData),
        indexToRemove = null;

    $.each(data['cities'], function(idx, obj){
        if (obj['id'] == id) {
            indexToRemove = idx;
            return false;
        }   
    });

    if (indexToRemove !== null) {
        data['cities'].splice(indexToRemove, 1);
    }

    var Count = 0;
    localStorage.setItem(Count, JSON.stringify(data));  
    JsonData = localStorage.getItem(Count);

Need Your Help

Using Dojos EnhancedGrid with client-sided plugins

dojo dgrid

I have got an EnhancedGrid with a JsonRestStore bound to a backend Rest API. Thats my code for it:

What’s the best approach when migrating legacy projects across versions of visual studio?

visual-studio migration legacy

I've been thinking about the number of projects we have in-house that are still being developed using visual studio 6 and how best to migrate them forward onto visual studio 2008. The projects rang...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.