2

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"
            }
        ]
    }

3 Answers 3

21

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'd be cautious about removing elements from an array while iterating through it, especially if using a method like jQuery.each() that caches the .length before it starts...
A complete solution is always nice, won't ever deny one of those :-D however something as a foot hold always work to. This I think works for me well. Ill be testing this out shortly enough, thanks
if a match is found I would likely remove it after the loop, catching which specific object set it was in the above mentioned concept as a variable to pass to something like delete if found. Its a good foot hold for me to run with, and thats all I could have asked for.
the deleted item becomes undefined in the array
4

You could write a removeFirst function like this:

function removeFirst(arr, func)
    {
        for (var i=0; i<arr.length; i++)
        {
            if (func.call(arr[i]))
            {
                arr.splice(i,1);
                return arr;
            }
        }
    }

and then call it:

removeFirst(locations, function(){return this.zipcode=="06238";});

If you want to remove more then one element, you could write it like this:

function removeMany(arr, func)
    {
        for (var i=arr.length; i>0; --i)
        {
            if (func.call(arr[i]))
            {
                arr.splice(i,1);
            }
        }
        return arr;
    }

and use it in the same way.

Alternatively, use underscore (http://documentcloud.github.com/underscore) reject method in a similar way:

_.reject(locations, function(location){ return location.zipcode=="06238";});

Underscore is pretty good for doing array manipulations.

1 Comment

For loop in Removemany() should be for (var i=arr.length; i>0; --i).
0

It worker for me.

  arrList = $.grep(arrList, function (e) { 

        if(e.add_task == addTask && e.worker_id == worker_id) {
            return false;
        } else {
            return true;
        }
    });

It returns an array without that object.

Hope it helps.

Comments

Your Answer

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.