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