javascript - Delete array by index -


i'm having little trouble js delete() function.

straight chrome inspector:

> x = [{name: 'hello'}, {name: 'world'}] > [object, object] > delete x[0] > true > $.each (x, function (i, o) {console.log(o.name);}) > typeerror: cannot read property 'name' of undefined > x > [undefined × 1, object] 

do have idea why happening? it's causing me issues each

the delete() method on array data structure little misleading. when following:

var = ['one', 'two', 'three']; delete a[0]; 

delete() similar assigning array element undefined. note after using delete(), array not shifted , length remains same:

a.length -> 3 a[0] -> undefined 

so in essence, delete() creates sparse array , not alter length property nor remove element. remove element, want following:

a.splice(0,1) 

this both remove element , alter array's length property. now:

a.length -> 2 

see the splice method details on method arguments.


Comments