| by Arround The Web | No comments

Remove Null Values From an Array in JavaScript

In arrays, removing null values (including empty strings, null, undefined, and so on) is an important task for developers. To do so, JavaScript provides some methods, such as a traditional for loop that will iterate the array and add the values in a new array, the forEach() method, or the filter() method.

This tutorial will illustrate the methods for removing null values from JavaScript arrays.

How to Remove Null Values From JavaScript Array?

To remove null values from an array, use the below-given JavaScript built-in methods:

Method 1: Remove Null Values From JavaScript Array Using filter() Method

Use the “filter()” method for removing null values from an array. It creates and returns a new array by adding the elements that fulfill the requirements specified in the function.

Syntax

Follow the given syntax for the filter() method:

array.filter(function(currentValue, index, array))

 
In the above syntax:

    • A “function” that will be executed for every element of the array.
    • The “currentValue” displays the current element’s value.
    • index” is the presently selected element’s index.
    • array” is the array containing the current element.
    • index” and “array” are the optional arguments, while “function” and “currentValue” are mandatory.

Example

Create an array containing numbers and null values:

var array = [1, , "", 2, 56, 13, 44, null, undefined, 85];

 
Call the filter() method with an arrow function as a call back function which selects only the elements that will not equal to the keyword “null” and stores it resulting in variable “res”:

var res = array.filter(elements => {
 return elements !== null;
});

 
Print the resultant array on the console:

console.log(res);

 
The output displays an array that removes the “null” value:


Now, remove all the null, undefined, and empty values from an array, and add these multiple conditions with the AND “&&” operator:

var res = array.filter(elements => {
 return (elements != null && elements !== undefined && elements !== "");
});

 
The given output shows that all the specified values from an array are successfully removed:

Method 2: Remove Null Values From JavaScript Array Using for Loop

Use the most traditional method to iterate the array using the “for” loop with the “push()” method. It will iterate the array until its length and push the elements into a newly created array.

Syntax

The following syntax of the “for” loop is used for removing null values from an array:

for () {
// return statements
}

 
Example

First, create an empty array:

var resArray = [];

 
Use the for loop to iterate the array and get the non-null values and add them to a newly created array using the “push()” method:

for (let i = 0; i < array.length; i++) {
 if (array[i]) {
  resArray.push(array[i]);
 }
}
console.log(resArray);

 
The output indicates that the for loop efficiently returns the array excluding the null values:

Method 3: Remove Null Values From an Array Using forEach() Method

For removing null values from an array, use the “forEach()” method with the “push()” method. For each element of an array, it calls a callback function.

Syntax

Follow the given-provided syntax for the forEach() method:

array.forEach(function(currentValue, index, arr)

 
In the above syntax,

    • A function to execute for every element of the array.
    • The “currentValue” is the current element’s value.
    • index” is the index of a presently selected element.
    • array” is the array containing the current element.
    • function” and “currentValue” are mandatory, while “index” and “array” are the optional parameters.

Example

Create an empty array to hold all array elements that are not null:

var resArray = [];

 
Then, call the “forEach()” method to iterate the array and verify that the element under consideration is not null inside the forEach() method. Then, push that element of the array into the empty array if the criteria are met:

array.forEach(elements => {
 if (elements !== null) {
  resArray.push(elements);
 }
});

 
Print the resultant array on the console:

console.log(resArray);

 
The output shows an array without the “null” value:


Now, eliminate all the other undefined values from an array by specifying the following condition:

array.forEach(elements => {
 if (elements != null && elements !== undefined && elements !== "") {
  resArray.push(elements);
 }
});

 
Output


We compiled all the best methods for removing the null values from an array in JavaScript.

Conclusion

To remove null values from an array, use the JavaScript prebuilt methods, including the “filter()” method, “for” loop, or “forEach()” method. The array.filter() method is the most used method for removing the null values from an array. The “for” loop is the traditional method to iterate the array and add the elements except for null values in an array using the “push()” method. This tutorial illustrated the methods for removing null values from JavaScript arrays.

Share Button

Source: linuxhint.com

Leave a Reply