Home > Mobile >  How to access a nested object in GEOJSON with mapbox
How to access a nested object in GEOJSON with mapbox

Time:08-15

I have a very large geojson that I am reading in via mapbox tile server. I can get data up to "statistics" but do not know how to access the objects after that. So how

 map.on('click', 'area-polygon', (e) => {
        console.log(e.features[0].properties.statistics);
  });

This is the current console.log() output;

{"runs":
    {"byActivity":
        {"downhill":
            {"byDifficulty":
                {"easy":
                    {"count":17,
                    "lengthInKm":14.4112,
                    "minElevation":1553.6,
                    "maxElevation":1999.59,
                    "combinedElevationChange":1996.04},
                "advanced":
                    {"count":4,
                    "lengthInKm":2.33805,
                    "minElevation":1645.08,
                    "maxElevation":1936.27,
                    "combinedElevationChange":628.717},
                "intermediate":
                    {"count":11,
                    "lengthInKm":8.24159,
                    "minElevation":1553.6,
                    "maxElevation":2000.42,
                    "combinedElevationChange":1542.16},
                "other":
                    {"count":5,
                    "lengthInKm":2.42858,
                    "minElevation":1595.16,
                    "maxElevation":2005.37,
                    "combinedElevationChange":208.312}
                }
            }
        },
        "minElevation":1553.6,
        "maxElevation":2005.37  
    },
    "lifts":
        {"byType":
            {"chair_lift":
                {"count":3,
                "lengthInKm":3.82236,
                "minElevation":1553.6,
                "maxElevation":1998.59,
                "combinedElevationChange":863.786
                },
            "magic_carpet":
                {"count":1,
                "lengthInKm":0.962681,
                "minElevation":1580.79,
                "maxElevation":1742.16,
                "combinedElevationChange":161.378},
            "gondola":
                {"count":1,
                "lengthInKm":2.07562,
                "minElevation":1580.44,
                "maxElevation":1998.42,
                "combinedElevationChange":417.985}
            },
            "minElevation":1553.6,
            "maxElevation":1998.59
        },
    "maxElevation":1998.59,
    "minElevation":1553.6
}

So how would I access the count under easy under byDifficulty etc ? This doesn't work:

e.features[0].properties.statistics.runs.byActivity.downhill.byDifficulty.easy.count

CodePudding user response:

There's something odd here - if your first log output is correct, your second code would work.

Normally, Mapbox GL JS converts nested objects in GeoJSON into strings.

So you need to do something like:

JSON.parse(e.features[0].properties.statistics).runs.byActivity.downhill.byDifficulty.easy.count
  • Related