How to find JSON data based on other content in a JSON object when duplicates exist?
P粉713846879
P粉713846879 2024-04-04 10:57:45
0
1
519

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粉713846879
P粉713846879

reply all(1)
P粉668019339

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template