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:
|
|||||||||||||||||||||
|
Pure javascript:
or - (only for arrays items which are numbers is numbers' strings)
or - (only for single array items of type "text")
or - Classic way: simple iteration
via jQuery:
UPDATE - just another fast, cool way (using ES6):
|
||||
If you need to remove ALL empty values ("", null, undefined and 0):
Example:
Return:
|
|||||||||||||
|
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 |
|||||||||||||||||||||
|
Simply one liner:
or using underscorejs.org:
|
|||||||||||||||||
|
The clean way to do it.
|
|||||||||
|
@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.
|
|||||||||
|
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 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:
|
|||
|
What about that:
|
|||||
|
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:
|
|||
|
Filtering out invalid entries with a regular expression
|
||||
|
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)
|
|||
|
You'll need use some form of iteration to accomplish this. There isn't any built in mechanism in JavaScript to accomplish the task. |
|||
|
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 :-
|
|||||
|
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
|
|||
|
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
|
|||||||||||||||||||||
|