I'm currently working on a function that takes a key (specified as a string) and retrieves its corresponding values from a given JSON object.
Here is the JSON data I am working with:
[
{
"some_key1": [
{"key": "value1"},
{"key": "value2"},
{"key": "value3"}
]
},
{
"some_key2": [
{"key": "value4"},
{"key": "value5"},
{"key": "value6"}
]
},
{
"default_val": [
{"key": "value7"},
{"key": "value8"},
{"key": "value9"}
]
}
]
The structure of this JSON may vary, but it always consists of inner objects.
Below is the implementation of a function that will retrieve the array of values based on the provided key:
interface InnerObject {
key: string;
}
const fetchValues = (key = "default_val"): InnerObject[] => {
/* actual code goes here */
}
Expected output example:
> fetchValues("some_key2");
[{"key": "value4"},{"key": "value5"},{"key": "value6"}]
> fetchValues();
[{"key": "value7"},{"key": "value8"},{"key": "value9"}]
Looking forward to any elegant solutions!