var my_arr = [4, '', 0, 10, 7, '', false, 10];
my_arr = my_arr.filter(Boolean);
console.log(my_arr);
Output
[4,10,7,10]
If you are using ES6 in your project, you can easily remove empty or falsy values from an array. It is the fastest way to do that.
const input_arr = [20, 30, , , , null, 50, 40, undefined, , null, undefined];
const result = [];
for (let i of input_arr)
i && result.push(i);
console.log(result)
Output
[20,30,50,40]
0 Comments