The Wayback Machine - https://web.archive.org/web/20210420013639/https://www.geeksforgeeks.org/javascript-remove-empty-elements-from-an-array/
Skip to content
Related Articles

Related Articles

JavaScript | Remove empty elements from an array
  • Last Updated : 22 Apr, 2019

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:
    Image
  • After clicking on the button:
    Image

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:
    Image
  • After clicking on the button:
    Image

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :