Apologies if the question is worded weird, but it is much easier to explain with an example.
Suppose I have a json containing
[
{
"UID": 84001001,
"iso2": "US",
"iso3": "USA",
"code3": 840,
"FIPS": 1001,
"Admin2": "Autauga",
"Province_State": "Alabama",
"Country_Region": "US",
"Lat": 32.53952745,
"Long_": -86.64408227,
"Combined_Key": "Autauga, Alabama, US",
"1/22/20": 0,
... so on and so on ...
"5/27/21": 21606
},
{
"UID": 84001003,
"iso2": "US",
"iso3": "USA",
"code3": 840,
"FIPS": 1003,
"Admin2": "Baldwin",
"Province_State": "Alabama",
"Country_Region": "US",
"Lat": 30.72774991,
"Long_": -87.72207058,
"Combined_Key": "Baldwin, Alabama, US",
"1/22/20": 0,
... so on and so on ...
"5/27/21": 21606
},
...
]
and this json continues for all counties for all US states.
I have two react state variables, USState
and county
. If we are iterating through every currState
in the json and USState === currState.Province_State
and county === currState.Admin2
, then I want that object.
Example: if USState = "Alabama"
and county = "Baldwin"
, then this object will contain:
dataObject = {
"UID": 84001003,
"iso2": "US",
"iso3": "USA",
"code3": 840,
"FIPS": 1003,
"Admin2": "Baldwin",
"Province_State": "Alabama",
"Country_Region": "US",
"Lat": 30.72774991,
"Long_": -87.72207058,
"Combined_Key": "Baldwin, Alabama, US",
"1/22/20": 0,
... so on and so on ...
"5/27/21": 21606
}
I already have this part done. I have a dataObject
like above. I do not want to include my code for that because that's more related to React and this question is geared more towards JavaScript and json.
Now, I want to filter through this object to only have the dates, so now dataObject
will contain
dataObject = {
"1/22/20": 0,
... so on and so on ...
"5/27/21": 21606
}
then finally, I want to construct an array based on these key value pairs to get
array = [
{ x : new Date(2020, 01, 22), y : 0},
...,
{ x : new Date(2021, 05, 27), y : 21606}
]
so I can plot it using Canvas.js
.
However, as a beginner with react, I find myself getting confused and roadblocked, constantly using map
and filter
functions while also getting used to this style of programming. I think I'm making this way harder or more complicated than it needs to be. This is also my first time dealing with json.
Basically, I just want to filter my dataObject
to only contain keys with dates, then plot these dates (keys) vs their number (value). I would appreciate any help. Thank you