简体   繁体   English

如何从 onChange 事件中过滤 json 数组中的多个 json 项目

[英]How to filter multiple json items in a json array from onChange event

I have a react-select component that I am taking multiple values from.我有一个react-select组件,我从中获取多个值。 I am then trying to find all matches in a JSON array.然后我试图在 JSON 数组中找到所有匹配项。 I been trying for a while cant figure out the best way.我尝试了一段时间无法找出最好的方法。 I want to filter and print the matches.我想过滤并打印匹配项。 Below is the data.下面是数据。 i want to get all conditions that match the selected symptoms.我想获得与所选症状相匹配的所有条件。

export const symptomsCondition = [
  {
    condition: "Acetaminophen And Nsaid Toxicity",
    symptions: ["Disorganized Speech", "Insomnia"],
  },
  {
    condition: "Acne",
    symptions: ["Swelling (Axilla)", "Bleeding Easily", "Difficult To Wake Up", "Increased Thirst"],
  },
  {
    condition: "Adrenal Disorders (Addison’S Disease And Cushing’S Syndrome",
    symptions: ["Weakness (Leg)", "Tremor", "Premature Ejaculation"],
  },
  {
    condition: "Age Related Cognitive Decline",
    symptions: ["Swelling (Jaw)", "Furry Tongue", "Headache (Worst Ever)", "Mucus In Eyes"],
  },
  {
    condition: "Alcohol: Reducing The Risks",
    symptions: [
      "Pulling Out Eyebrows",
      "Bleeding (Toes)",
      "Craving To Eat Ice",
      "Weakness (Shoulder)",
      "Pounding Heart",
    ],
  },
  {
    condition: "Allergies",
    symptions: ["Thick Saliva", "Unable To Grip (Hands)", "Irregular Heartbeat"],
  },

 const [yoursymptoms, setYourSymptoms] = useState(null);

  useEffect(() => {
    symptomsCondition.symptoms.find((yoursymptoms) => 
    console.log(yoursymptoms);
    )
  }, [yoursymptoms]);

onChange={setYourSymptoms}

You have to use filter on your array with the required criterias.您必须在阵列上使用具有所需条件的filter Something like:就像是:

const allConditions = symptomsConditions.filter(c => c.symptions.includes(yoursymptoms))

And if you want only conditions, you can chain this with如果你只想要条件,你可以用

.map(c => c.conditon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM