You have to loop backwards, because every time you splice
, the length and indexes change:
for (var i = stringArray.length-1; i>=0; i--) {
if(stringArray[i] === ""){
stringArray.splice(i, 1);
}
}
Alternate solution with Array.prototype.filter
(documentation page provides a shim for old browsers that won't support this method):
stringArray = [ "","","Banana"];
var a = stringArray.filter(function(item) {
return item !== '';
});
console.log(a);