![](/img/trans.png)
[英]How Do I access a variable inside a variable inside a function in Javascript?
[英]how do I access a variable inside a variable ? (javascript )
我在這里遇到了一些問題,我正在創建一個 Leaflet 項目並且需要訪問變量之外的數據,在我的例子中是一個多邊形。 直到現在我有多邊形內的數據
data:[28.63,34.29,12.23,9.33,6.97,3.93,4.63]
但我更希望數據在這個變量之外
var koeln = [28.63,34.29,12.23,9.33,6.97,3.93,4.63];
我現在正在尋找如何訪問數據中的 var koeln 的結果,我試圖將它放在剎車或其他東西上,但似乎沒有任何效果..
編輯:
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 ],
(等等)
參考你的評論,你有這個數據結構:
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"
}
}]
};
您可以像這樣訪問數據:
let koeln = wahl2017.features[0].properties.data
您可以使用var koeln = wahl2017.features[0].properties.data
訪問數據變量
當您有更多城市時,您可以調用.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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.