Remove different items from JS array

I’m trying to solve an exercise where I need to create the function cleanArray and remove various items from an array. This is as far as I could get

function cleanArray(arr) {

    for (var i = 0; i < arr.length; i++) {

        if (arr[i] === null || 0 || "" || false || undefined) {
            arr.splice(i, 1);

            return arr

But this only filters out null for me. I’ve tried putting the items to remove in a separate array or going one by one, but then it either returns undefined or only filters out the 0 even when it’s not the first item to remove.

I’ve seen other answers where people use filter or indexOf but I want to see if there’s a way to do it with splice. Is it not possible to use the logical operators here?

7 thoughts on “Remove different items from JS array”

Leave a Comment