In my React Native application, I am dealing with a JSON file containing various data sets. One of the challenges I am facing is extracting specific information from this JSON and displaying it correctly.
[
{
"001": [
{
"index": 0,
"fare": 0,
"city": "Colombo"
},
{
"index": 1,
"fare": 30,
"city": "Maligawaththa"
},
{
"index": 2,
"fare": 38,
"city": "Kelanithissa"
},
{
"index": 3,
"fare": 50,
"city": "4 Kanuwa / Biyagama road"
},
{
"index": 4,
"fare": 61,
"city": "Thorana Junction"
},
{
"index": 5,
"fare": 73,
"city": "Kelaniya University"
}
]
},
{
"100": [
{
"index": 0,
"fare": "446.00",
"city": "Mulgampola"
},
{
"index": 1,
"fare": "452.00",
"city": "Kandy"
}
]
},
{
"101": [
{
"index": 0,
"fare": "446.00",
"city": "Mulgampola"
},
{
"index": 1,
"fare": "452.00",
"city": "Kandy"
}
]
}
]
I have attempted to extract the keys and cities from each object without success. The goal is to display these keys in a Picker
element and then show the corresponding cities in a separate DropDown
element when a key is selected.
const keys = data.map((item) => Object.keys(item)[0]);
const cities = data.flatMap((item: any) => {
const routeNumber = Object.keys(item)[0];
return item[routeNumber].map((subItem: any) => ({
label: subItem.city,
value: subItem.index,
}));
});
If anyone could help me correct this issue and make the data extraction process work as intended, it would be greatly appreciated.