Open In App

How to remove specific value from array using jQuery ?

Last Updated : 24 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array elements and the task is to remove the specific value element from the array with the help of JQuery. There are two approaches that are discussed below:

Approach 1: We can use the not() method which removes the element that we want. Later, use get() method to get the rest of the elements from the array.

  • Example:




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Remove certain property for all objects
            in array with JavaScript.
        </title>
      
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color: green">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <button onclick="gfg_Run()">
            Click Here
        </button>
      
        <p id="GFG_DOWN" style="color:green;"></p>
      
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var arr = ["GFG", "GeeksForGeeks", "Geek", "Geeks"];
            var remEl = "Geek";
            el_up.innerHTML = "Click on the button to perform "
                    + "the operation.<br>Array - [" + arr + "]";
                      
            function gfg_Run() {
                var arr2 = $(arr).not([remEl]).get();
                el_down.innerHTML = "[" + arr2 + "]";
            
        </script>
    </body>
      
    </html>

    
    

  • Output:

Approach 2: We can use the inArray() method to get the index of the element (that is to be removed) and then use slice() method to get the rest of the elements.

  • Example:




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Remove certain property for all objects
            in array with JavaScript.
        </title>
      
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color: green">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <button onclick="gfg_Run()">
            Click Here
        </button>
      
        <p id="GFG_DOWN" style="color:green;"></p>
      
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var arr = ["GFG", "GeeksForGeeks", "Geek", "Geeks"];
            var remEl = "Geek";
            el_up.innerHTML = "Click on the button to perform "
                    + "the operation.<br>Array - [" + arr + "]";
                      
            function gfg_Run() {
                arr.splice($.inArray(remEl, arr), 1);
                el_down.innerHTML = "[" + arr + "]";
            
        </script>
    </body>
      
    </html>

    
    

  • Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads