2

I have data about 250 cities and their population in a GeoJSON file.

{ 
"type": "FeatureCollection",
"name": "municipial_data",
"crs": { 
    "type": "name",
    "properties": { 
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84" 
        } 
    },
    "features": [ { 
        "type": "Feature",
        "properties": { 
            "dataset": "population",
            "name": "CityName",
            "data": { 
                "stable": { 
                    "y_1994": "15489",
                    "y_2000": "15524",
                    "y_2005": "15694",
                    "y_2010": "15371",
                    "y_2015": "15095",
                    "y_2019": "14895" 
                    },
                "stable_0_5": { 
                    "y_1994": "731",
                    "y_2000": "789,
                    "y_2005": "791",
                    "y_2010": "701",
                    "y_2015": "683",
                    "y_2019": "594" 
                    },
                } 
            } 
        },
        "geometry": { 
            "type": "MultiPolygon",
            "coordinates": 
            [ 
                [ 
                    [ 
                        [ 20.0848926, 47.208004], [20.0893161, 47.1978454], .........
                    ] 
                ] 
            ] 
        } ]
    ...

I load it to the layer named dataLayer of a map created with Openlayers v6.6.1. Later in the code I'd like to get the properties, with the dataLayer.getSource().getFeatures()[0].get("name") method e.g. I can get the name property of the first city, it works.

Please, help, how can I get the value for the year 1994 (y_1994) from the stable property group? I have tried dataLayer.getSource().getFeatures()[0].get("data[stable][y_1994]"), but only undefined returns.

Pál Rudan
  • 123
  • 4
  • 1
    `dataLayer.getSource().getFeatures()[0].get("data").stable.y_1994` – Mike Sep 13 '21 at 21:14
  • Thank you, it solved. Based on this answer (https://stackoverflow.com/a/4255480/12096766) the object properties' names could also come from a variable, function return value, etc., when using bracket notation. – Pál Rudan Sep 14 '21 at 08:19
  • 1
    this is true, but the notation would be `dataLayer.getSource().getFeatures()[0].get("data")["stable"]["y_1994"]` or `dataLayer.getSource().getFeatures()[0].get("data")["stable"].y_1994` if you want to mix. You can only get one layer deep, then you need another point (`.`) – Rob Sep 14 '21 at 10:57

0 Answers0