In javascript it is easy to write an array using literals
var arreglo = [1, 2, 3];
This will create an array of size 3 where I can iterate through all its elements with a for loop.
for (var i = 0; i < arreglo.lenght; i++) {
console.log(arreglo[i]);
}
But there's a problem
I can declare an array like this
var arreglo = new Array(3);
or do something like this
delete arreglo[1];
which will create a sparse array with holes
Now when I go to test if the array has a value at that position I can do something like
for (var i = 0; i < arreglo.lenght; i++) {
if (arreglo[i]) {
// haz algo
}
// o también
if (arreglo[i] !== undefined) {
// haz algo
}
}
but then if i do
arreglo[2] = undefined;
the above will fail catastrophically even though the array has a value at that position
How can I detect if there is actually a gap or has been assigned the value of undefined
at that position?
If you assign
to compare you should use
To compare the delete you could use
delete operator
validate the title
Eliminando elementos de un array