What is the best method to get specific keys (i.e. "longName") from nested objects with this structure:
const routes = {
"14094": {
"routeId": 14094,
"shortName": "2",
"longName": "Route 1"
},
"14095": {
"routeId": 14095,
"shortName": "7",
"longName": "Route 2"
},
"14096": {
"routeId": 14096,
"shortName": "42",
"longName": "Route 3"
},
"14097": {
"routeId": 14097,
"shortName": "57",
"longName": "Route 4"
},
I have tried to console.log(routes[1]), dot notation, and other methods but I am not having any success.
The structure here is an object of objects. So you can access the longName key-value pair in this manner: routes[14094].longName which should return Route 1, based on the original data.
If you want to get all the longName values for each object, you can use a for loop like this:
for (const key in routes) {
console.log(routes[key].longName)
}
You need to first reference the object using the "outer" key (e.g. 14094), then you can use dot notation for the keys in the "inner" object.