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? |
||||
|
I use this method, extending the native Array prototype:
Or you can simply push the existing elements into other array (the example removes every "falsy" value:
|
|||||||||||||||||||||
|
You'll need use some form of iteration to accomplish this. There isn't any built in mechanism in JavaScript to accomplish the task. |
|||
|
You'll have to loop and then splice() |
|||||||||
|
Try this. Pass it your array and it will return with empty elements removed. *Updated to address the bug pointed out by Jason
|
|||||||||||||||||||||
|
You may find it easier to loop over your array and build a new array out of the items you want to keep from the array than by trying to loop and splice as has been suggested, since modifying the length of the array while it is being looped over can introduce problems. You could do something like this:
Actually here is a more generic solution:
You get the idea - you could then have other types of filter functions. Probably more than you need, but I was feeling generous... ;) |
||||
|
If you've got Javascript 1.6 or later you can use
since The MDN page linked above also contains a nice error-checking version of Note that this will not remove |
|||||||||||||||||||||
|
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)
|
|||
|
@Alnitak Actually Array.filter works on all browsers if you add some extra code. See below.
This is the code you need to add for IE, but filter and Functional programmingis worth is imo.
|
||||
Filtering out invalid entries with a regular expression
|
||||
|
Pure javascript:
or - (only for single array items of type "text")
or - Classic way: simple iteration
via jQuery:
UPDATE - just another fast, cool way (using ES6):
|
|||||||||||||||||||||
|
What about that:
|
|||||
|
The clean way to do it.
|
|||||||||
|
If you need to remove ALL empty values ("", null, undefined and 0):
To remove empty values and Line breaks:
Example:
Return:
UPDATE (based on Alnitak's comment) In some situations you may want to keep "0" in the array and remove anything else (null, undefined and ""), this is one way:
Return:
|
|||||||||||||||||||||
|
If using a library is an option I know underscore.js has a function called compact() http://documentcloud.github.com/underscore/ it also has several other useful functions related to arrays and collections. Here is an excerpt from their documentation:
|
|||||
|
I needed to do this same task and came upon this thread. I ended up using the array "join" to create a string using a "_" separator, then doing a bit of regex to:-
...then using array "split" to make a cleaned-up array:-
...or in 1 line of code:-
...or, extending the Array object :-
|
|||||
|
I'm simply adding my voice to the above “call ES5's Specifically, ES5's
However, writing out
|
|||||||||||||
|
With Underscore/Lodash: General use case:
With empties:
|
|||||
|
Simply one liner:
or using underscorejs.org:
|
|||||||||||||||||||||
|
Another way to do it is to take advantage of the length property of the array : pack the non-null items on the 'left' of the array, then reduce the length. It is an in-place algorithm -does not allocates memory, too bad for the garbage collector-, and it has very good best/average/worst case behaviour. This solution, compared to others here, is between 2 to 50 times faster on Chrome, and 5 to 50 times faster on Firefox, as you might see here : http://jsperf.com/remove-null-items-from-array The code below adds the non-enumerable 'removeNull' method to the Array, which returns 'this' for daisy-chaining :
|
|||||
|
Nice ... very nice We can also replace all array values like this
|
|||
|
returns
|
||||
|
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 is another way to do it:
|
|||||||||
|
Since nobody else mentioned it and most people have underscore included in their project you can also use
|
|||
|
Here is an example using variadic behavior & ES2015 fat arrow expression:
|
||||
|