Hello Friends 👋,
Welcome to Infinitbility.
In this tutorial, we will see how we can remove spaces from an array in javascript.
To remove space from the array we will use multiple javascript concepts like an iterated array, remove useless space, and falsy methods.
JavaScript provides a filter()
method to filter array elements by some condition, where we will write our space condition.
filter()
method returns a new array based on your condition.
let take the following array example.
1var arr = ['Apple', ' ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
Here, I will share which I got two better methods to filter space from the array.
1var arr = ['Apple', ' ', 'Mango', '', 'Banana', ' ', 'Strawberry'];23// ES64arr = arr.filter(e => String(e).trim());56// ES57arr = arr.filter(function(e) {8 return String(e).trim();9});
Output

Thanks for reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.