Navigate back to the homepage

How to remove blank space from an array in javascript?

Infinitbility
JavaScript
January 19th, 2022 · 1 min read
keep affilitate

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'];
2
3// ES6
4arr = arr.filter(e => String(e).trim());
5
6// ES5
7arr = arr.filter(function(e) {
8 return String(e).trim();
9});

Output

Javascript remove space from array example
Javascript remove space from array example

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.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to remove backslash from json object in javascript?

Javascript, delete backslash from json object example.

January 18th, 2022 · 1 min read

How to remove an object from an array in javascript

Javascript, Delete object from array example.

January 17th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]