How do I remove empty elements from an array in JavaScript?
Is there a straightforward way, or do I need to loop through it and remove them manually?
How do I remove empty elements from an array in JavaScript?
Is there a straightforward way, or do I need to loop through it and remove them manually?
When using the highest voted answer above, first example, i was getting individual characters for string lengths greater than 1. Below is my solution for that problem.
Instead of not returning if undefined, we return if length is greater than 0. Hope that helps somebody out there.
Returns
This works, I tested it in AppJet (you can copy-paste the code on its IDE and press "reload" to see it work, don't need to create an account)
Simple ways:
or - (only for single array items of type "text")
or - Classic way: simple iteration
via jQuery:
UPDATE - just another fast, cool way (using ES6):
Remove empty values
What about that:
Filtering out invalid entries with a regular expression
This might help you : https://lodash.com/docs/4.17.4#remove