I am just a beginner in JavaScript. I have been stuck with this question on arrays in Javascipt where we have to remove all the false value from an array. I have attached an image for the sameJavaScript Array Question
https://codepen.io/tsiruot/pen/NWxLmGV?editors=0012
console.clear();
var arr = [NaN, 0, 15, false, -22, '', undefined, 47, null, 94]
var i
for (i = 0; i < arr.length; i++) {
if (!arr[i]) {
arr.splice(i, 1)
}
}
console.log(arr)As you see in this codepen, The problem is if(!arr[i]) is not considering 0 and undefined as true. I am using splice to delete the elements. Where am i going wrong?``
Aucun commentaire:
Enregistrer un commentaire