Javascript – jquery javascript remove object data from JSON object

javascriptjqueryjsonobject

I have JSON Object that looks something like the below object, this object can go on for days. So I am wondering is there anyway I can delete full set a set being the equivalent of locations[0] or locations[1] in the example below. I'd have to first iterate over the Object and try to figure out which one is which though. So lets say I am looking to remove an set where the zipcode is 06238, I would need to run over the entire locations object and find out which set it is in the object, then remove it accordingly. Question is I'm not sure how to approach that notion.

{
"locations": [
            {
                "city": "San Jose",
                "state": "CA",
                "zipcode": "95125",
                "longitude": "0",
                "latitude": "0"
            },
            {
                "city": "Coventry",
                "state": "CT",
                "zipcode": "06238",
                "longitude": "0",
                "latitude": "0"
            }
        ]
    }

Best Answer

Simply just pass the index

delete locations[0];

You go through a normal JSON iteration like this

jQuery.each(locations, function(i, val) {
   if(val.zipcode == "yourvalue") // delete index
   {
      delete locations[i];
  }
});

something like that. are you looking for an idea or a complete function.

Here is the jsFiddle