How to filter object depending on the field’s value in JavaScript ?

Given an object and the task is to filter the object depending upon the field’s value in JavaScript. There are two approaches that are discussed below.

Approach 1: First create an empty object with the field’s value as key. Then one by one traverse the original object and append the object at the proper key of the object. Use the push() method to push the object at the end of the array in the new object.

  • Example: This example implements the above approach.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
        <h1>GeeksForGeeks</h1>
          
        <p id="GFG_UP"></p>
          
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
          
        <script>
            var up = document.getElementById("GFG_UP");
            var obj = [
                { ID: 2, Name: 'name_2', title: 'title_a' },
                { ID: 1, Name: 'name_1', title: 'title_a' },
                { ID: 3, Name: 'name_1', title: 'title_c' }
            ];
      
            up.innerHTML = "[" + JSON.stringify(obj) + "]";
            var down = document.getElementById("GFG_DOWN");
              
            function myGFG() {
                var titles = {};
                for (var i = 0; i < obj.length; i++) {
                    titles[obj[i].title] = [];
                }
                for (var i = 0; i < obj.length; i++) {
                    titles[obj[i].title].push(obj[i]);
                }
                down.innerHTML = JSON.stringify(titles);
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

Approach 2: First, create an empty object with the field’s value as key with empty array as value using new Array() . Then one by one traverse the original object and append the object at the proper key. Use the push() method to push the object at the end of the array in the new object.

  • Example: This example implements the above approach.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
        <h1>GeeksForGeeks</h1>
          
        <p id="GFG_UP"></p>
          
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
      
        <script>
            var up = document.getElementById("GFG_UP");
            var obj = [
                { ID: 2, Name: 'name_2', title: 'title_a' },
                { ID: 1, Name: 'name_1', title: 'title_a' },
                { ID: 3, Name: 'name_1', title: 'title_c' }
            ];
            up.innerHTML = "[" + JSON.stringify(obj) + "]";
            var down = document.getElementById("GFG_DOWN");
              
            function myGFG() {
                var names = {};
                for (var i = 0; i < obj.length; i++) {
                    if (!names[obj[i].Name]) {
                        names[obj[i].Name] = new Array();
                    }
                    names[obj[i].Name].push(obj[i]);
                }
                down.innerHTML = JSON.stringify(names);
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.