Open In App

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

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are 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 the 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.

html




<head>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <h1 style="color:green">
        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


Output: 

filter object depending on the field's value in JavaScript

filter object depending on the field’s value in JavaScript

Approach 2: First, create an empty object with the field’s value as a key with an empty array as a value using the 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.

html




<head>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <h1 style="color:green">
        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>


Output: 

filter object depending on the field's value in JavaScript

filter object depending on the field’s value in JavaScript



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

Similar Reads