Removing undefined values from Array

In certain situations, it may happen that we have undefined or generally falsy values in Array structures. For instance when reading and filling data from some unknown sources like Databases or HTML structures. Like

var data = [42, 21, undefined, 50, 40, undefined, 9]

Since that might cause trouble when looping over such arrays and working on the elements, what is the best practice to remove undefined (falsy values) ?



To use Array.prototype.filter here might be obvious. So to remove only undefined values we could call

var data = [42, 21, undefined, 50, 40, undefined, 9];

data = data.filter(function( element ) {
   return element !== undefined;

If we want to filter out all the falsy values (such as 0 or null) we can use return !!element; instead.

But we can do it slighty more elegant, by just passing the Boolean constructor function, respectively the Number constructor function to .filter:

data = data.filter( Number );

That would do the job in this instance, to generally remove any falsy value, we would call

data = data.filter( Boolean );

Since the Boolean() constructor returns true on truthy values and false on any falsy value, this is a very neat option.


Inline using lambda

result.filter(item => item);

You can use lodash compact method, which removes null, undefined and ''


If you have an array of objects and want to remove all null and undefined items:

[].filter(item => !!item);

Array.prototype.reduce() can be used to delete elements by condition from an array but with additional transformation of the elements if required in one iteration.

Remove undefined values from array, with sub-arrays support.

function transform(arr) {
    return arr.reduce((memo, item) => {
        if (typeof item !== "undefined") {
            if (Array.isArray(item)) item = transform(item);
            // We can transform item here.
        return memo;
    }, []);

let test1 = [1, 2, "b", 0, {}, "", , " ", NaN, 3, undefined, null, 5, false, true, [1, true, 2, , undefined, 3, false, ''], 10];


Try it on

var a =  ["3","", "6"];
var b =  [23,54,56];
var result = [];

for (var i=0;i<a.length;++i) {
    if (a[i] != "") {
        result[i] = b[i];

result = result.filter(function( element ) {
   return element !== undefined;


As Diogo Capela said, but where 0 is not filtered out as well.

[].filter(item => item !== undefined && item !== null)

in ES6 this can be achieved by simply using using filter with function return the value like this:

const array = [NaN, 0, 15, false, -22, '',undefined, 47, null];
const filteredArr = array.filter(elm => elm);
var arr1 = [NaN, 0, 15, false, -22, '',undefined, 47, null];

var array1 = arr1.filter(function(e){ return e;});


single lined answer

var data = Object.keys(data)

This will remove undefined values but array index will change


ES6 single line

data.filter(e => e)


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.