JavaScript | Remove empty elements from an array
In order to remove empty elements from an array, filter() method is used. This method will return a new array with the elements that pass the condition of the callback function.
array.filter() This function creates a new array from a given array consisting of those elements from the provided array which satisfy conditions by the argument function.
array.filter( function(cValue, index, arr), tValue )
Parameters: This function accepts two parameter which are listed below:
- function: It contains three parameters.
- cValue: This parameter is required and it is the value of the current element.
- index: This parameter is optional.It is the array index of current element.
- arr: This parameter is optional. It is the array object the current element belongs.
- tValue: This parameter is optional. This value is to be passed to function to used as its “this” value.
If it is empty, the value “undefined” will be passed.
Example: This example is removing undefined, null and empty elements from the array.
<!DOCTYPE html> <html> <head> <title> JavaScript | Remove empty elements from an array </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="GFG_up" style="color:green;"></p> <button onclick = "myGeeks()"> Click here </button> <p id="GFG_down" style="color:green;"></p> <script> var array = ["GFG_1", "GFG_2", null, "GFG_3", "", "GFG_4", undefined, "GFG_5",,,,,, "GFG_6",, 4,, 5,, 6,,,,]; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = array; function myGeeks() { var p_down = document.getElementById("GFG_down"); var filtered = array.filter(function (el) { return el != null; }); p_down.innerHTML = filtered; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example:This example removes only “false” values from the array.
<!DOCTYPE html> <html> <head> <title> JavaScript | Remove empty elements from an array </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="GFG_up" style="color:green;"></p> <button onclick = "myGeeks()"> Click here </button> <p id="GFG_down" style="color:green;"></p> <script> var array = ["GFG_1", "GFG_2", null, "GFG_3", "", "GFG_4", undefined, "GFG_5", ,,,,, "GFG_6",, 4,, 5,, 6,,,,]; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = array; function myGeeks() { var p_down = document.getElementById("GFG_down"); var filtered = array.filter(function (el) { return el; }); p_down.innerHTML = filtered; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Program to remove empty array elements in PHP
- How to empty an array in JavaScript?
- Check if an array is empty or not in JavaScript
- Remove all the child elements of a DOM node in JavaScript
- JavaScript | Remove the last item from an array
- How to remove an element from an array in JavaScript?
- How to get all unique values (remove duplicates) in a JavaScript array?
- PHP | Remove duplicate elements from Array
- JavaScript | Add new elements at the beginning of an array
- How to check an object is empty using JavaScript?
- How to unpack array elements into separate variables using JavaScript ?
- How to check empty/undefined/null string in JavaScript?
- Best way to initialize empty array in PHP
- Delete the array elements in JavaScript | delete vs splice
- How to remove CSS property using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



