1

I have an array with the following structure :

var y = [{id:12,count:10}, {id:23,count:89}, {id:21,count:100},]

How can I remove element with id:23 ?

I am not willing to use to create a prototype method on the array Array.prototype.remove

Any pointers appreciated

Thanks

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
Anirban
  • 589
  • 3
  • 16
  • 40
  • 1
    possible duplicate of [How do I remove an object from an array with JavaScript?](http://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript) and http://stackoverflow.com/questions/6891329/remove-entry-from-javascript-array-object – Felix Kling Apr 24 '12 at 18:23

3 Answers3

5

ES5 code:

y = y.filter(function( obj ) {
    return obj.id !== 23;
});

ES5 is widely supported by all major browsers. Still, you might want to include on of the several Shims to backup old'ish browsers

jAndy
  • 231,737
  • 57
  • 305
  • 359
3
for (i in y) {
    if (y[i].id == 23) {
       y.splice(i, 1);
       break;
    }
}
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
  • 1
    if it's possible for there to be two elements with the matching id then this could fail (if they dups are in sequence), and if there is only one then there should be a `break` inside the if block. – quodlibetor Apr 24 '12 at 18:25
  • 1
    its pretty bad to use a `for in` to iterate over an array. You might end up iterating methods/functions or propertys on the `Array` aswell. Better use a standard `for` loop. – jAndy Apr 24 '12 at 18:25
  • In all major browsers variable i will never have property name of Array object. – Denis Ermolin Apr 24 '12 at 18:28
  • @DenisErmolin: on what grounds are you making that claim? – Elias Van Ootegem Apr 24 '12 at 18:30
  • Very good question. That is just wrong.. of course **ANY** browser will list the object properties aswell. Easy example: `var foo = [1,2,3]; foo.IAMBOSSBITCH = true;` loop over that... – jAndy Apr 24 '12 at 18:39
  • Sure it will but it will not iterate through standard properties. This approach is not bad for me because i never modify standard objects. Sure if you modifying them then use for(;;) loop – Denis Ermolin Apr 24 '12 at 18:42
  • beyond that, `for in` is much more slow than `for` or `while` :p http://jsperf.com/fiosdfhsoudfhodsf – jAndy Apr 24 '12 at 19:29
0

Denis Ermolin's answer is an option, though a few problems might occur, here's what I suggest:

for(var i=0;i<y.length;i++)
{
    if (y[i].hasOwnProperty('id') && y[i].id === 23)
    {
        delete(y[i]);
        break;
    }
}

When using arrays, it's always better to avoid the for - in loop, as this will loop through the Array.prototype, so i might suddenly contain length, not an index number.

When you're dealing with objects, the for in loop is a great thing, but again: it loops through the prototypes, that's why you're better off using the hasOwnProperty method.

The rest is pretty strait forward, I think... good luck

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149