javascript
Remove empty, false, undefined values from array Javscript
If you want to remove all false, empty, undefined values from the Javascript array you can use the code example and methods explained in this post.
var my_arr = [4, '', 0, 10, 7, '', false, 10];
my_arr = my_arr.filter(Boolean);
console.log(my_arr);
Output
[4,10,7,10]
Use For Loop and ES6 to remove falsy values from an Array
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]
Was this helpful?
Similar Posts
- Remove all false, null, undefined and empty values from array using Lodash
- Check if a variable is null, empty, undefined or false Javascript
- Javascript code to remove falsy value like null, undefined, false from array
- Remove items starting from last untill given condition is false Lodash
- Remove items begin from start until given condition is false Lodash
- Remove duplicate values from an array in Javascript
- Remove duplicates from an array and return unique values in O(n) complexity