Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there,

I want to make people able to search an article database with multiple criteria(s). It's a little bit different to handle because:

Some includes more than 1 value (like active substances: ["actSubst1", "actSubs2"])
Some values are repeating (retrospective: "1" for "true; ward_focused: "1" for true (again))
Here's my data, it's OK if you convert it into an array and work on it,

json
JavaScript
[
 {
  "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"
  }
 }
]


And here's my query:

JavaScript
const selectedFilters = {
      retrospective: ["1"],
      ward_focused: ["2"],
      indication_focused: ["1"],
      active_substance: []
  };


Simple solutions did not work for these two reasons:

I'm using ["1"] and ["2"] for the boolean data, same value (e.g. ["1"]) repeats itself inside a profile,
There may be more than one value ( e.g. "active_substance": ["Morphine", "Fentanyl"] )
I'm open to work on both as JSON and array for my data. Also I may modify the "1" -"2" structure for my boolean data types. You may also change the data structure if needed by removing the “filters” detail and working on one-depth.

Any help is appreciable. Best regards

What I have tried:

The closest solution was when I've converted my data into an array and work on it as:

JavaScript
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);


BUT it failed for the reason that the "1"s "true" and the "2"s for false are repeating many times in every profile.
Posted
Updated 19-Feb-20 20:05pm
Comments
Member 15627495 10-Nov-22 10:50am    
you have accurate parameters, why don't you reuse all those to make a form and go for search ?
your fields will be 'empty' or 'valued'.

one more thing : you always have the pair : key:value , implement a search with key and its value .. you'll never meet one more problems this way.

as you search just for "1"/"0" you'll encount confusion, but with te key in your filters, it always pass.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900