0

I can't delete all the undefined indexes in this array.I do console.log and it shows me [undefined, 2].

var array = [];
array[1] = 2;
array[3] = 4;

for(var i = 0; i < array.length; i ++) {
  if(array[i]) {

  } else {
    array.splice(array.indexOf(array[i]), 1);
  }
}

console.log(array)

1 Answers1

0

var array = [];
array[1] = 2;
array[3] = 4;

for(var i = 0; i < array.length; i ++) {
  if(array[i]) {

  } else {
    array.splice(i, 1);
  }
}

console.log(array)
Jose Marques
  • 748
  • 1
  • 6
  • 22
  • 1
    This doesn't fix anything. There should also be an explanation of what causes the OP's issue and how it fixes it (which should highlight why this doesn't work in a general case); – RobG Apr 10 '17 at 01:11