Remove undefined values from array Javascript

In this article, we will explore different methods to remove undefined values from an array in Javascript. If your array contains multiple undefined values and you want to clean it up efficiently, we’ll show you various methods to achieve this with simple example.

1. Remove undefined values from array Javascript using filter() method:

The easiest and most efficient method to remove undefined values is by using the filter() method. It creates a new array containing only elements that satisfy the given condition.

Example 1:
var newarr = [1, 2, undefined, 3, undefined, 4];

newarr = newarr.filter(function(e) {
    return e !== undefined;
});

console.log(newarr);
Example 2:

This is a concise and efficient method to filter out undefined values, as .filter() creates a new array with only the elements that meet the specified condition.

var newarr = [1, 2, undefined, 3, undefined, 4];

const filteredArray = newarr.filter(item => item !== undefined);

console.log(filteredArray);

Explanation:

  • array.filter(item => item !== undefined) goes through each element in  newarr.
  • The condition item !== undefined ensures only non-undefined values are included in filteredArray.
2. Remove undefined values from array Javascript using a loop

Another method is to use a loop to iterate through the array and remove undefined values.

Example 1:
var newarr = [1, 2, undefined, 3, undefined, 4];

for (var i = 0; i < newarr.length; i++) {
    if(newarr[i] === undefined) {
        newarr.splice(i, 1);
        i--;
    }
}

console.log(newarr);
Example 2:

for loop gives you more control and can be helpful if you need to perform additional operations on each element.

var newarr = [1, 2, undefined, 3, undefined, 4];

const results = []; 

for (var i = 0; i < newarr.length; i++) {
    if(newarr[i] !== undefined) {
        results.push(newarr[i]);
    }
}

console.log(results);
3. Remove undefined values from array using Javascript forEach loop

You can use ‘forEach’ loop and push non-undefined values into new array.

var newarr = [1, 2, undefined, 3, undefined, 4];

let c_arr = [];

newarr.forEach(itemarr => {
    if(itemarr !== undefined) {
        c_arr.push(itemarr);
    }
});

console.log(c_arr);
4. Remove undefined values from array Javascript using reduce() Method

You can use ‘reduce’() method to remove undefined values from an array. It is used for iterating over an array and accumulating a single value based on the array elements.

var newarr = [1, 2, undefined, 3, undefined, 4];

let c_arr = newarr.reduce((item1, element1) => {
    if(element1 !== undefined) {
        item1.push(element1);
    }
    return item1;
}, []);

console.log(c_arr);
5. Using the for... in loop

Another method is to remove undefined values from an object is to use a for … in loop. This method will iterate over the object’s properties and check if the value is undefined, if it’s undefined it will delete it.

const dataValue = {
    language: "Javascript",
    extension: undefined,
    profession: "developer",
    type: "A"
};

for(let key in dataValue) {
    if(dataValue[key] === undefined) {
        delete dataValue[key];
    }
}
console.log(dataValue);
  • ‘filter()’ method is the most of used approach.
  • ‘forEach()’ and ‘reduce’ provide alternative non-mutating solutions.
  • ‘splice()’ mutates the original array.
  • ‘for …in loop’ object is to use a for … in loop

I hope this tutorial help you.

Jasminkumar Borad
Jasminkumar Borad

I'm a full-stack developer, enterpreneur and owner of webstuffsolution.com. I live in india and i like to write tutorial and tips that can help to other user. I like to share various programming language tips and solution. I believe in hardworking and consistency.

Leave a Reply

Your email address will not be published. Required fields are marked *