Rosh
Rosh

Reputation: 1736

removing data by value from a json array

I had created two arrays to store an array of data into local storage. I'm storing it by creating an array of array. But not able to remove the value.

 `var divMappingArray = new Array();
 var data = new Array();

 data[0] = {"ID": 123, "Name": "temp" };
 divMappingArray.push(data[0]);
 data[1] = {"ID": 23, "Name": "temp1" };
 divMappingArray.push(data[1]);`

 localStorage.setItem('zoneObject', JSON.stringify(divMappingArray));

I'm saving this into local storage, but I need to remove a value based on name or id. I tried looping it and remove the item, but this is not working for me.

  $.each($.parseJSON(retrievedObject), function(i, value){
            delete value.ID[i];
    });

Parsing the array value by retrieving from local storage and want to add the updated data into local storage back. While removing i want to remove the whole set of array value both ID and name, the retrieved data length will be reduced after the removal.

Upvotes: 0

Views: 93

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

After modifying the aray, you need to update the local storage

var rarray = $.parseJSON(retrievedObject);
$.each(rarray, function (i, value) {
    if (value.ID == 23) {
        rarray.splice(i, 1);
        return false;
    }
});
localStorage.setItem('zoneObject', JSON.stringify(rarray));

Demo: Fiddle

Upvotes: 1

Related Questions