How to remove empty elements from an array in JavaScript?
Hi! I'm a beginner in JavaScript and encountered a problem when working with arrays. I have an array that comes from a form or data parsing. It contains empty strings, null, undefined, and just empty elements. I need to get a clean array with only real data.
Here's an example:
```javascript
const messyArray = ['apple', '', 'banana', null, 'cherry', undefined, 'date', ' ', false, 0];
```
I want to get the output: `['apple', 'banana', 'cherry', 'date']`.
I tried doing this:
```javascript
const cleanArray = messyArray.filter(item => item);
```
But this only removes `null` and `undefined`, while empty strings and `false` remain. Then I tried:
```javascript
const cleanArray = messyArray.filter(item => item !== '');
```
But this doesn't remove `null` and `undefin
No answers yet.
Log in to leave an answer