OneCompiler

[Javascript] How to remove empty strings from an array without any loops?

I want to remove the empty strings present in an array without using any loops. For example,
['this', 'is', '', 'a', '', 'test', '', 'message'] to ['this', 'is', 'a', 'test', 'message'].
How can I do that in Javascript?

1 Answer

4 years ago by

You can use filter() method to filter out the empty strings from the given array as below.

let arr = ['this', 'is', '', 'a', '', 'test', '', 'message'];

console.log(arr.filter(ele => ele!= ''));

Run here https://onecompiler.com/javascript/3xnpjwabg

4 years ago by Meera