I'm trying to pull data from a JSON file depending on the user input that has come from various checkboxes. An example of my JSON file is below;
{
"factor": 1,
"pathway": 2,
"title": "Lorem impsum",
},
{
"factor": 3,
"pathway": 2,
"title": "Lorem impsum",
},
{
"factor": 4,
"pathway": 2,
"title": "Lorem impsum",
},
I first loop through the values of the checkboxes to get an array for each category of user input so i'll end up with two arrays that look something like this.
factorArray = [1,3,4]
pathwayArray = [2]
What would be the best way to compare these arrays during the fetch request, and pull relevant JSON where the two values relate? So for this example all three of the JSON objects would be returned.
My fetch code currently looks like this:
function getFactors(e) {
e.preventDefault();
var checkboxesPath = document.querySelectorAll('#pathway:checked')
var pathwayArray = [];
// Retrieve user input from checkboxes
for (var i = 0; i< checkboxesPath.length; i++) {
pathwayArray.push(checkboxesPath[i].value)
}
var checkboxesFactor = document.querySelectorAll('#factors:checked')
var factorsArray = [];
for (var i = 0; i< checkboxesFactor.length; i++) {
factorsArray.push(checkboxesFactor[i].value)
}
fetch("pathway.json")
.then(res => res.json())
.then(data => {
let output = ``;
var results = data.filter(obj => factorsArray.includes(obj.factor) && pathwayArray.includes(obj.pathway))
console.log(results);
})
}
I've tried looping through the arrays while in the map function but cannot get it to work for multiple values, in plain english I'm hoping to get something like this
where factorArray[i] == pathwayArray[i] {
push relevant JSON to DOM
}