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:
|
|||||||||||||||||||||
|
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
Remove falsely valuesAnother method that removes the
|
|||||||||||||||||||||
|
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:
|
|||||||||||||||||||||
|
Simply one liner:
or using underscorejs.org:
|
|||||||||||||||||||||
|
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 |
|||||||||||||||||||||
|
The clean way to do it.
|
|||||||||
|
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:
|
|||||
|
Since nobody else mentioned it and most people have underscore included in their project you can also use
|
|||
|
With Underscore/Lodash: General use case:
With empties:
|
|||||||||
|
@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... ;) |
||||
|
What about this(ES6) : To remove Falsy value from an array.
|
|||
|
What about that:
|
|||||
|
I'm simply adding my voice to the above “call ES5's Specifically, ES5's
However, writing out
|
|||||||||||||
|
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)
|
|||||
|
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 :
|
|||||
|
Filtering out invalid entries with a regular expression
|
|||||
|
returns
|
||||
|
'Misusing' the for ... in (object-member) loop. => Only truthy values appear in the body of the loop.
|
|||
|
Nice ... very nice We can also replace all array values like this
|
|||
|
Here is an example using variadic behavior & ES2015 fat arrow expression:
|
||||
|
The best way to remove empty elements, is to use Unfortunately,
|
||||
|
How about doing it this way
|
|||||
|
If anyone is looking for cleaning the whole Array or Object this might help.
Output: Removes anything that is jsfiddle here |
|||
|
This one will only remove empty values and not falsey ones, which I think is more desirable. There is an option to also remove null values. This method should be much faster than using splice.
|
|||
|
You'll have to loop and then splice() |
|||||||||||||
|
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 :-
|
|||||
|
This is another way to do it:
|
|||||||||||||
|
Try this. Pass it your array and it will return with empty elements removed. *Updated to address the bug pointed out by Jason
|
|||||||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
var a = [,,]
andvar a = [undefined, undefined]
. The former is truly empty, but the latter actually has two keys, but withundefined
values. – Alnitak Dec 29 '15 at 11:30