On the frontend the user will click on multiple options and all that will be stored in an array as objects as shown in dataToFilter
variable below. Then when user clicks on search button, the array will be sent to the backend and the api route will run the algorithm filtering the data based on the items in the array and return the filtered data to the user. My question – how can I run a filter loop on the data matching the items in the array to search for? One way I can think of is checking the length of the array and setting the number of &&
accordingly within the filter loop but I dont think that would be the appropriate way to go about it. Thank you for reading this post.
***Dummy Data To Search From***
let data = [
{"name":"Testing", "age":32,"score": 95, "gender":"male"},
{"name":"Testing", "age":22,"score": 85, "gender":"female"},
{"name":"Testing", "age":32,"score": 85, "gender":"male"},
{"name":"Testing", "age":52,"score": 95, "gender":"female"},
]
***Filter Data***
let dataToFilter = [{"age":32},{"score": 95}]
let filteredData = []
filteredData = data.filter((item)=>{
return item.age === 32 && item.score === 95
)