How do I remove empty elements from an array in JavaScript?
Is there a straightforward way, or do I need to loop through it and remove them manually?
How do I remove empty elements from an array in JavaScript?
Is there a straightforward way, or do I need to loop through it and remove them manually?
Just
ES6
and newer versions method, assume array is below:Simple way:
ES6: let newArr = arr.filter(e => e);
What about this(ES6) : To remove Falsy value from an array.
Simply one liner:
or using underscorejs.org:
The clean way to do it.
@Alnitak
Actually Array.filter works on all browsers if you add some extra code. See below.
This is the code you need to add for IE, but filter and Functional programmingis worth is imo.