This is an attempt to further a previous question.. I feel like this is just different enough to warrant another topic in hopes to help anyone who is trying to solve the same problem.
If I have a dataset of key-value pairs, let's say I want to accomplish 3 things:
- Find the first occurrence of a value of an inner key-value pair.
- Copy that value into a Map
- Utilize the value of another key-value pair as the key for the Map.
So, for example, let's say I have the following dataset:
[
{"date":"2019-01-01", "temp":"cold", "season":"winter", "precip":"snow"},
{"date":"2019-02-01", "temp":"cold", "season":"winter", "precip":"none"},
{"date":"2019-03-01", "temp":"mild", "season":"spring", "precip":"rain"},
{"date":"2019-04-01", "temp":"mild", "season":"spring", "precip":"none"},
{"date":"2019-05-01", "temp":"warm", "season":"spring", "precip":"rain"},
{"date":"2019-06-01", "temp":"warm", "season":"summer", "precip":"hail"},
{"date":"2019-07-01", "temp":"hot", "season":"summer", "precip":"none"}
]
I would like to end up with the following Map object:
[
"2019-01-01" => "snow",
"2019-02-01" => "none",
"2019-03-01" => "rain",
"2019-06-01" => "hail"
]
As a last challenge, how could I do this in a function where my result can be dynamic? So in the above example, I chose 'precip' as the desired value in the final Map. But what if I wanted 'season'? Is there a way to do that dynamically, where I can pass the 'key' name as an argument of a function?
Also, is there a name for this operation? I had trouble coming up with a title. If someone has a better idea, I'd gladly rename it. I feel like this is an elegant problem that many may run into.
"2019-06-01" => "hail"?"2019-06-01"and it is associated with the value"hail".