const my_array = [10, false, null, "devsheet", undefined];
const filtered_array = my_array.filter(Boolean);
// -> [10,"devsheet"]
We are using Array.filter(Boolean) to filter out falsy values from the array. We have created an array my_array that contains the blank strings, false, null, and undefined items in it. It will filter all these values and return a new array in the filtered_array variable.
Live Demo
const fruits = ["apple", false, "banana", null, undefined];
const filtered_fruits = fruits.filter(function (value) {
return value ? true : false;
});
console.log(filtered_fruits);
0 Comments