how do I access a variable inside a variable ? (javascript )

Viewed 85

I'm having a bit of a problem here, I'm creating a Leaflet project and need to access to data outside of a variable, in my case a polygon. Until now I have the data inside the polygon with

data:[28.63,34.29,12.23,9.33,6.97,3.93,4.63]

but I'd prefer the data to be outside of this variable with

var koeln = [28.63,34.29,12.23,9.33,6.97,3.93,4.63];

I'm now looking for a result how to access to the var koeln inside of the data, I've tried to put it in brakes or other things, but nothing seems to work..

edit:

  var wahl2017 =  { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": 
        {"name":"Wahlbezirk 13","partei":"cdu","wahlbeteiligung":69.42, "drittpa1":"gruen","drittpa2":"fdp", data:[28.63,34.29,12.23,9.33,6.97,3.93,4.63],title:"Köln I"}, "geometry": { "type": "Polygon", "coordinates": [ [ [6.976650953292847, 50.84119137806263 ],

(and so on)

2 Answers

Refering to your comment, you have this data structure:

var wahl2017 = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "name": "Wahlbezirk 13",
            "partei": "cdu",
            "wahlbeteiligung": 69.42,
            "drittpa1": "gruen",
            "drittpa2": "fdp",
            data: [28.63, 34.29, 12.23, 9.33, 6.97, 3.93, 4.63],
            title: "Köln I"
        }
    }]
};

You can access the data like this:

let koeln = wahl2017.features[0].properties.data

You can access the data variables with var koeln = wahl2017.features[0].properties.data

When you have more cities you can call .forEach()

var cities = [];
wahl2017.features.forEach(function(city){
    cities.push({name: city.properties.name, data: city.properties.data});
})

console.log(cities);
console.log(cities[0].data);
Related