0

How can I remove userid 2 from the following array using javascript

{"maxPages":2,"data":[{"UserId":"1","UserName":peterparker,"}{"UserId":"2","UserName":spiderman,"}]}

I was thinking of first getting the index like: id = 2 row = myarray.data.UserId.indexOf(id)

Then remove the row based on the index

6
  • Array.prototype.splice Commented Aug 21, 2013 at 18:14
  • Always use delete on array elements. Besides, is there not multiple syntax errors with this declaration? Commented Aug 21, 2013 at 18:18
  • @MartinAndersson delete does not change array length by shifting elements to fill the space Commented Aug 21, 2013 at 18:19
  • 1
    @MartinAndersson Never use delete with arrays. Use delete with objects, and use splice() with arrays (or generate a new array). Commented Aug 21, 2013 at 18:21
  • If you don't care about the sort order, then just iterate the Array, and when you find the one you're looking for, relocate the last item in the Array to that index, and then reduce the .length by 1. Commented Aug 21, 2013 at 18:49

4 Answers 4

2
var obj = {}; // ...your object

for ( var i = 0; i < obj.data.length ) {
    if ( obj.data[i].UserId == 2 ) {
        obj.data.splice(i, 1);
        break;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

obj.data[i].UserId == 2 not obj.data[i].UserId = 2
@MoazzamKhan - Yuck! Maybe I should switch to Yoda notation :)
Yoda notation is so ugly :D
@MoazzamKhan - It sure is, but it'll prevent errors like these.
0

There are several possibilies:

  1. Array#splice, but you need to know index of corresponding entry.
  2. Array#filter, which is slightly less efficient, but more convinient:

    var obj = {"maxPages":2,"data":[{"UserId":"1","UserName":peterparker,"}{"UserId":"2","UserName":spiderman,"}]};
    obj.data = obj.data.filter(function(v) { return v.UserId != 2 });
    

4 Comments

How is .filter() less efficient?
I suppose it'd act pretty straightforward, like: create a new array, push required items, return this new array, delete old one. Whereas splice would need only to remove one element and shift remaining, which can be done without mem. allocations.
While you may think that, splice is slow compared to other methods: jsperf.com/splice-vs-filter
For me (Chrome 28.0.1500.95 on Linux 64-bit) splice performs a bit faster than built-in filter, thought hand-written filter (named push in testcase) is 4 times faster. Also I'm not sure about various compiler optimizations in this syntetic test case.
0

The one of joellustigman works for me, but it has two little grammar corrections, the i++ and the "==" instead of "="

var obj = {
"maxPages":2,
"data":[{
"UserId":"1",
"UserName":"peterparker"
},
{"UserId":"2",
"UserName":"spiderman"
}
]}

for ( var i = 0; i < obj.data.length; i++ ) {
    if ( obj.data[i].UserId == "2" ) {
        obj.data.splice(i, 1);
        break;
    }
}

Comments

0

You can do this:

myarray.data = myarray.data.filter(function(item) {
  return item.UserId !== 2;
});

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.