search

Home  >  Q&A  >  body text

How to find JSON data based on other content in a JSON object when duplicates exist?

I'm currently implementing some data charts on a website, and I have a JSON that contains some data, specifically some ID numbers, some dates, and some data points. Because I have multiple dates under the same ID, I'm looking for a way to do essentially "find a JSON object with this ID and month, then get the data point number from that object". Any help on how I can achieve this? If possible, it is best not to iterate as the files are very large. I will provide an example of JSON:

[{
"ID" : "32",
"month" : "6",
"value" : "438"
},
{
"ID" : "32",
"month" : "5",
"value" : "223"
}

It should also be noted that in the actual json the IDs are not grouped together like this, this is just an example. Also, changing the json content is not an option.

Any help is very important Thank you very much!

P粉713846879P粉713846879261 days ago564

reply all(1)I'll reply

  • P粉668019339

    P粉6680193392024-04-05 09:15:27

    In JS you can use array.find.

    Similar to res = myArray.find(item => item["ID"] === "32" && item["month"] === "5"). p>

    Then res is the object you want to search for.


    If you want to complete the lookup without any iteration, you need to generate a data structure with unique keys, ie. Create a new object with keys similar to id-month.

    reply
    0
  • Cancelreply