One of the challenges posed here is:
- Some values include more than one item (like active substances: ["Morphine", "Fentanyl"])
- Some values repeat themselves (retrospective: "1" for "true"; ward_focused: "1" for true (again))
Unfortunately, the previous solutions I attempted did not suit my project.
jsonData
[
{
"title": "Real-world evidence of high-cost drugs for metastatic melanoma",
"url": "https://.../Suppl_1/A5.1",
"filters": {
"retrospective": "1",
"ward_focused": "2",
"indication_focused": "1",
"active_substance": "2"
}
},
{
"title": "Real-world safety and tolerability of the recently commercialised palbociclib",
"url": "https://.../Suppl_1/A223.2",
"filters": {
"retrospective": "2",
"ward_focused": "1",
"indication_focused": "2",
"active_substance": "Palbociclib"
}
},
{
"title": "Cost-effectiveness of morphine versus fentanyl in managing ventilated neonates",
"url": "https://.../Suppl_1/A7.3",
"filters": {
"retrospective": "1",
"ward_focused": "1",
"indication_focused": "1",
"active_substance": ["Morphine", "Fentanyl"]
}
},
{
"title": "Chemical risk assessement in a quality control laboratory",
"url": "https://.../Suppl_1/A9.2",
"filters": {
"retrospective": "2",
"ward_focused": "2",
"indication_focused": "2",
"active_substance": "2"
}
},
{
"title": "The economic burden of metastatic breast cancer in Spain",
"url": "https://.../27/1/19",
"filters":{
"retrospective": "1",
"ward_focused": "1",
"indication_focused": "1",
"active_substance": "2"
}
}
]
query
const selectedFilters = {
retrospective: ["1"],
ward_focused: ["2"],
indication_focused: ["1"],
active_substance: []
};
The approach that came close to providing a solution involved converting the data into an array and manipulating it as follows:
const filterArr = Object.values(selectedFilters).flat();
const output = myDataArray.filter(({filters}) => {
const objFilters = Object.values(filters).flat();
return filterArr.every(val => objFilters.includes(val));
})
console.log(output);
However, this method failed due to the repetitive occurrences of "1" for "true" and "2" for false in each profile.
I am willing to work with both JSON and array for my data. I may also consider modifying the "1" -"2" structure for my boolean data types. If necessary, the data structure can be altered by removing the “filters” detail and working on a single level.
Any assistance provided would be greatly appreciated. Best regards