I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, \"\", undefined, and NaN.
This should be what you are looking for:
let array = [7, 'ate', '', false, 9, NaN];
function removeFalsyItems(array) {
// Your result
let filter = array.filter(Boolean);
// Empty the array
array.splice(0, array.length);
// Push all items from the result to our array
Array.prototype.push.apply(array, filter);
return array
}
removeFalsyItems(array) // => [7, 'ate', 9], funny joke btw...