Remove undefined values from array Javascript

In this article, we will explain how to remove undefined values from array Javascript. If you have an array with many undefined values and you want to remove all of them, we will show you how to do it. Below is an example of how to remove undefined from array javascript.

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

In Javascript, you can remove undefined values from an array using various methods. One of the easiest method to do this is by using the ‘filter()’ method. Let’see an example below.

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.

Leave a Reply

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