EN
JavaScript - remove empty strings from array
3 points
In this article, we would like to show you how to remove empty strings from an array in JavaScript.
Quick solution:
xxxxxxxxxx
1
const outputArray = inputArray.filter(x => typeof x !== 'string' || x.length > 0);
In this example, we use filter()
method to create a copy of the letters
array without empty strings.
xxxxxxxxxx
1
const letters = ['A','', "",,,, 'B', 'C', 0, 1, 2];
2
const result = letters.filter(x => typeof x !== 'string' || x.length > 0);
3
4
console.log(result); // ['A', 'B', 'C', 0, 1, 2]