需要访问函数中定义的变量。嵌套循环是解决方案吗?
Need to access variable defined in a function. Is nested loop the solution?
我有以下代码,需要访问变量 "density" 并将其放在弹出窗口中。密度在 mapCities 函数中定义,但我添加的标记和我创建的弹出窗口是在 map cities 函数之外的 for 循环中创建的,导致 ReferenceError: density is not defined
index.html:151:42。这是预期的错误。我只是不知道如何访问该密度变量。应该使用嵌套循环吗?
function mapCities(units) {
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var density = calcPopDensity(cityNumPeeps, cityZone, units); // call calcPopDensity passing population, cityZone and units as arguments
console.log(density);
}
};
function calcPopDensity(cityNumPeeps, cityZone, units) {
if (units == "miles") {
return cityNumPeeps / cityZone // calculate population density in miles and return it
} else if (units == "km") {
return cityNumPeeps / cityZone * 1.60934 // calculate population density in miles and return it
}
};
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var popup = `<b>${cityName}</b><br>
<b>population</b>: ${density}<br>`
L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
.bindPopup(popup);
console.log(popup)
//if statement determines units to place in popup text
if (units == "miles") {
popup += `miles`; // add text if if var density returns miles
} else if (units == "km")
popup += `kilometers`; // add text if if var density returns kilometers
L.marker(cityXY).addTo(map) // add popup again based on if statement
.bindPopup(popup);
console.log(popup)
}
```
只需在需要的地方为 for 循环的局部密度创建一个变量。
above
密度变量超出范围,因此您将无法按原样使用它。
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var density = calcPopDensity(cityNumPeeps, cityZone, units);
var popup = `<b>${cityName}</b><br>
<b>population</b>: ${density}<br>`
L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
.bindPopup(popup);
console.log(popup)
//if statement determines units to place in popup text
if (units == "miles") {
popup += `miles`; // add text if if var density returns miles
} else if (units == "km")
popup += `kilometers`; // add text if if var density returns kilometers
L.marker(cityXY).addTo(map) // add popup again based on if statement
.bindPopup(popup);
console.log(popup)
}
我有以下代码,需要访问变量 "density" 并将其放在弹出窗口中。密度在 mapCities 函数中定义,但我添加的标记和我创建的弹出窗口是在 map cities 函数之外的 for 循环中创建的,导致 ReferenceError: density is not defined index.html:151:42。这是预期的错误。我只是不知道如何访问该密度变量。应该使用嵌套循环吗?
function mapCities(units) {
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var density = calcPopDensity(cityNumPeeps, cityZone, units); // call calcPopDensity passing population, cityZone and units as arguments
console.log(density);
}
};
function calcPopDensity(cityNumPeeps, cityZone, units) {
if (units == "miles") {
return cityNumPeeps / cityZone // calculate population density in miles and return it
} else if (units == "km") {
return cityNumPeeps / cityZone * 1.60934 // calculate population density in miles and return it
}
};
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var popup = `<b>${cityName}</b><br>
<b>population</b>: ${density}<br>`
L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
.bindPopup(popup);
console.log(popup)
//if statement determines units to place in popup text
if (units == "miles") {
popup += `miles`; // add text if if var density returns miles
} else if (units == "km")
popup += `kilometers`; // add text if if var density returns kilometers
L.marker(cityXY).addTo(map) // add popup again based on if statement
.bindPopup(popup);
console.log(popup)
}
```
只需在需要的地方为 for 循环的局部密度创建一个变量。
above
密度变量超出范围,因此您将无法按原样使用它。
for (var i = 0; i < cities.length; i++) {
var cityName = cities[i];
var cityNumPeeps = cityPops[i];
var cityZone = cityAreas[i];
var cityXY = cityCoords[i];
var density = calcPopDensity(cityNumPeeps, cityZone, units);
var popup = `<b>${cityName}</b><br>
<b>population</b>: ${density}<br>`
L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
.bindPopup(popup);
console.log(popup)
//if statement determines units to place in popup text
if (units == "miles") {
popup += `miles`; // add text if if var density returns miles
} else if (units == "km")
popup += `kilometers`; // add text if if var density returns kilometers
L.marker(cityXY).addTo(map) // add popup again based on if statement
.bindPopup(popup);
console.log(popup)
}