0

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.

asked Jan 16, 2014 at 6:48
2
  • I think the second data[0] = should be data[1] =. Commented Jan 16, 2014 at 6:51
  • @Barmar : yes it is 1 , im updated the same Commented Jan 16, 2014 at 6:57

1 Answer 1

1

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

answered Jan 16, 2014 at 6:52
Sign up to request clarification or add additional context in comments.

5 Comments

rarray length is same before and after removing.
@Rosh you are not removing anything from rarray, only a property inside the array objects are deleted
i want to remove the whole {"ID": 23, "Name": "temp1" }; based on id, sorry my question was not clear i guess.
can i get key also in the loop tried with key.Name, not wrking that
@Rosh which key... if you want to access name value.Name

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.