Skip to content
Advertisement

How to filter data using multiple values in React.JS

I have a huge set of JSON data which I consider to be pretty complex when it comes to filtering data out of it. The idea is to take inputs from the user from the District, Ward No, and Categories dropdowns and use those to filter out the data i.e (grievances, general, urgent, and service), and display their properties in the table below. So like let’s say if the selected value from the dropdown is “Grievances”, it should show all the properties from the grievances array in the table. I have tried using the filter method by taking hardcoded inputs to see if the console would print anything or not but apparently it doesn’t. I don’t think I’ll be able to move any further without support on this. Below, you will find a snippet of the dropdowns, the JSON and the code that I tried for testing.

enter image description here

P.S Please ignore the data on the table as it is only hardcoded. The picture is only to give you a basic idea of what I intend to do once I get the correct resolution for the code I have written for testing.

JSON(Please note that every district has 3 ward in total. For example, in this case, the ward is 6 which has the following arrays namely "grievance", "general", "urgent", "services")

JavaScript

The code(The query object is to store the values received from the dropdowns dynamically. This right now is hardcoded and once I have the solution, I believe I’ll be able to manage the rest)

JavaScript

Advertisement

Answer

First of all, I’d say your JSON data is purely structured. I mean ward_no. You have categories with the dynamic name and makes it harder to filter out data while the name ward is also there (which can be a category name as well). As a suggestion, if you have an option to modify that part like this below then consider changing it.

JavaScript

I tried to make a filter function for you based on the parameters you have defined (ward, district, category).

It filters out data based on AND comparison for example when ward, category are passed both have to match.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement