Not able to remove values in Array using the .splice() in Javascript
Posted: 2022-07-28, 14:13
I was trying to loop through an array and remove any values that equal false, null, 0, undefined, NaN, or "".
This is how it should work - When I call the function it should return a blank array but right now it's outputting [null,0,null,null,""]. Here's what I have so far.
What is wrong with the arr.splice() that I am making? I have used the .splice() exactly how it was mentioned here - https://www.scaler.com/topics/splice-in-javascript/ . But the elements are still there. Any pros here who can help?
This is how it should work - When I call the function it should return a blank array but right now it's outputting [null,0,null,null,""]. Here's what I have so far.
Code: Select all
function bouncer(arr) {
for(i=0;i<arr.length;i++){
if(arr[i]===false||null||0||undefined||NaN||""){
arr.splice(i,1);
}
}
return arr;
}
bouncer([false, null, 0, NaN, undefined, ""]);