0

I need to filter from json object as per user input using Javascript. I am explaining my json object below.

var orgdata={
            "zone_list":[{
                "zone":'NORTH',
                "state_list":[{
                    "state":"DELHI",
                    "location_list":[{
                        "location":"NEW DELHI",
                        "task_list":[{
                            "login_id":"9937229853"
                        },{
                           "login_id":"9937229854" 
                        }]
                    },{
                        "location":"AIRPORT",
                        "task_list":[{
                            "login_id":"9937229855"
                        }]
                    }]
                },{
                   "state":"JK",
                    "location_list":[{
                        "location":"NEW JK",
                        "task_list":[{
                            "login_id":"9937229856"
                        },{
                           "login_id":"9937229857" 
                        }]
                    },{
                        "location":"AIRPORT JK",
                        "task_list":[{
                            "login_id":"9937229858"
                        }]
                    }] 
                }]
            },{
                "zone":'EAST',
                "state_list":[{
                    "state":"WB",
                    "location_list":[{
                        "location":"KOLKATA",
                        "task_list":[{
                            "login_id":"9937229859"
                        },{
                           "login_id":"9937229850" 
                        }]
                    },{
                        "location":"ASAM",
                        "task_list":[{
                            "login_id":"9937229895"
                        }]
                    }]
                },{
                   "state":"ODISHA",
                    "location_list":[{
                        "location":"BHUBANESWAR",
                        "task_list":[{
                            "login_id":"9937229844"
                        },{
                           "login_id":"9937229845" 
                        }]
                    },{
                        "location":"AIRPORT BBSR",
                        "task_list":[{
                            "login_id":"9937229846"
                        }]
                    }] 
                }]
            }]
        }

Here My user input is var zoneInput='NORTH';var stateInput='DELHI';var locationInput='AIRPORT';. As per this input I should get the below expected output.

{
    "zone_list":[{
        "zone":'NORTH',
        "state_list":[{
            "state":"DELHI",
            "location_list":[{
                "location":"AIRPORT",
                "task_list":[{
                    "login_id":"9937229855"
                }]
            }]
        }]
    }]
}

Here also user blank input will be checked and output will come accordingly.

  • 3
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). If you have an object or array, then you have an object or array, full stop. JSON format is a *method of representing an object in a string*, like `const myJSON = '{"foo":"bar"}'`. If there are no strings, serialization, or deserialization involved, then JSON is not involved either. – CertainPerformance Jan 21 '19 at 10:49
  • 1
    Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – kawashita86 Jan 21 '19 at 11:23

0 Answers0