0

I have a javascript object which looks like this :-

var myObject = [{"id": "1", "URL": "http://shsudhf.com", "value": "1"}, 

                {"id": "2", "URL": "http://shsusadhf.com", "value": "2"},

                {"id": "3", "URL": "http://shsudsdff.com", "value": "0"}];

Now , I have to delete all the rows in the object with id value 2. How can this be done ?

6
  • you can recreate the array without the deleted part Commented Dec 12, 2012 at 6:52
  • Does it have to be a mutate operation? Commented Dec 12, 2012 at 6:52
  • What do you have handy? prototype.js? jquery? Commented Dec 12, 2012 at 6:53
  • I was using plain javascript. Recreating will be a bit slow if the number of rows are very large, I guess. Yes, it will be mutually used Commented Dec 12, 2012 at 6:56
  • Do you need the original array after you remove the items with the id of "2"? Commented Dec 12, 2012 at 7:02

5 Answers 5

5

If you don't need the original array after "deleting" rows, you can use splice like this:

http://jsfiddle.net/rmcu5/1/

var myArray = [{"id": "1", "URL": "http://shsudhf.com", "value": "1"},
                {"id": "2", "URL": "http://shsusadhf.com", "value": "2"},
                {"id": "3", "URL": "http://shsudsdff.com", "value": "0"}];

function removeItemsById(arr, id) {
    var i = arr.length;
    if (i) {   // (not 0)
        while (--i) {
            var cur = arr[i];
            if (cur.id == id) {
                arr.splice(i, 1);
            }
        }
    }
}

removeItemsById(myArray, "2");

console.log(JSON.stringify(myArray));

It doesn't create a new array, just modifies the original in place. If you need the original array and all of its items, then use one of the other solutions that return you a modified copy of the original.

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

6 Comments

+1 for splice as this is the "native" way to remove items from an array.
Can you explain a bit about arr.splice ?
@PrashantSingh It takes 3 parameters (only the first 2 we're worried about). The first parameter is the index to start removing items from, and the second parameter is how many items to remove. I just set it as 1 because we only want to remove the single item in the loop (its id is 2)
@PrashantSingh The looping starts at the end of the array and loops to the beginning, which is important for something like this because you're modifying the array directly while looping through it and can throw off the looping
@PrashantSingh No problem. Just remember, this modifies the original array. If you want to keep the original array in its state but also get a copy that has specific items removed, you would need to use someone else's solution. It really depends on what you specifically need to do
|
5

Note that what you call myObject is actually an array therefore you can use array methods on it:

myObject = myObject.filter(function( obj ) {
  return obj.id != 2;
});

Demo: http://jsfiddle.net/elclanrs/LXpYj/

4 Comments

That's very nice but the OP did ask for a mutator. I don't agree with the OP's request for a mutator. Your solution is obviously the best, since removing from the middle of an array is awful. If the OP agrees that this is okay, I'll +1.
@PrashantSingh Note that this is not available on IE < 9. You'd have to include a polyfill just for the filter method to work on them
@Ian Ok, that means I can't use this :O
Yes but you need to add a polyfill for older browsers. A polyfill is just some code that adds that functionality if is not present. There's a 140bytes filter polyfill here: gist.github.com/1031656. Even with the polyfill the code will be shorter and more readable than most other ways of doing this.
2

try this:

function deleteObject(array,id)
{
 var newObject=[]; 
  for (var o in array) {
       if(array[o].id!=id)
          newObject.push(array[o]);
    }
return newObject;
}

working JS fiddle

You can do without creating new array, you need to write remove function:

Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

Without New Array Delete Object

1 Comment

Is recreating the array is the only solution ?
0

try it with filter (its an array not a object)

var rr = [{"id": "1", "URL": "http://shsudhf.com", "value": "1"},  {"id": "2", "URL": "http://shsusadhf.com", "value": "2"}, {"id": "3", "URL": "http://shsudsdff.com", "value": "0"}];


rr = rr.filter(function(e) {
    return e.id != 2;
});

Comments

-1

Here you go, this is without recreating the array or anything.

 var myObject = [{"id": "1", "URL": "http://shsudhf.com", "value": "1"}, 

                {"id": "2", "URL": "http://shsusadhf.com", "value": "2"},

    {"id": "3", "URL": "http://shsudsdff.com", "value": "0"}];

for(i=0,iMax=myObject.length;i<iMax;i++){
    (function (a) { 
        if(this.id=="2"){
          delete myObject[a];
        }
    }).call(myObject[i],i);
}

console.log(myObject);
​
​

jsFiddle

http://jsfiddle.net/gG2zz/1/

3 Comments

You do realize that THIS would do exactly the same, and it still would'nt remove anyting, it just sets the array items to undefined ?
@adeneo well I'm sorry if it's late and I'm tired. but why do you say it doesn't delete it link
delete works fine on objects, like in the linked question, but on arrays the index is not updated, and the length would still be the same, so it's not really "deleted", that's the main problem with arrays, deleting stuff is a pain in the a$$, and usually takes some slicing and dicing.

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.