javascript - Does 'array[i]=undefined' produce a sparse array? -


is delete array[i] same array[i] = undefined? know former produce sparse array? latter same?

no, latter not same, sets index value undefined, not delete index.

delete delete index, not update length, end index not defined, , doesn't contain value undefined.
in other words, "sparse" array length greater number of items.

you can test array methods, iterate on value undefined, not on indexes aren't defined (there's difference)

var arr = [0, 1, 2, 3];    delete arr[1]; // delete second index    arr.foreach(function(item) {    console.log(item); // 0, 2, 3  });    console.log('--------')    var arr2 = [0, 1, 2, 3];    arr2[1] = undefined; // set second index "undefined"    arr2.foreach(function(item) {    console.log(item); // 0, undefined, 2, 3  });


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -