if 数组元素在 for 循环内有条件
if array element conditional inside for loop
我在 JavaScript 的数组中返回了一系列位置,我正在 Google 地图上绘制这些位置。
我正在尝试根据数组元素之一的值更改标记图标的类型,例如
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
if (locations[i][3] == "Yes") {
console.log("yes")
} else {
console.log("no")
}
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
但 运行 变为
Uncaught SyntaxError: Unexpected token (
我错过了什么?
What am I missing?
您将流代码放在对象初始值设定项的中间:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
if (locations[i][3] == "Yes") { // ====
console.log("yes") // ====
} else { // ==== Here
console.log("no") // ====
} // ====
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
你不能那样做。 我不确定你想在那里做什么你发表了澄清评论:
within the new google.maps.Marker({ I need to be able to set icon: '/img/a.png' or icon: '/img/b.png' depending on the value of locations[i][3]
所以我对 属性 的猜测是正确的:您可以使用条件运算符来做到这一点:
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3] == "Yes" ? '/img/a.png' : '/img/b.png'
});
...如果 locations[i][3] == "Yes"
为真,icon
属性 的值将为 '/img/a.png'
,否则为 '/img/b.png'
。
我在 JavaScript 的数组中返回了一系列位置,我正在 Google 地图上绘制这些位置。
我正在尝试根据数组元素之一的值更改标记图标的类型,例如
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
if (locations[i][3] == "Yes") {
console.log("yes")
} else {
console.log("no")
}
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
但 运行 变为
Uncaught SyntaxError: Unexpected token (
我错过了什么?
What am I missing?
您将流代码放在对象初始值设定项的中间:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
if (locations[i][3] == "Yes") { // ====
console.log("yes") // ====
} else { // ==== Here
console.log("no") // ====
} // ====
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
你不能那样做。 我不确定你想在那里做什么你发表了澄清评论:
within the new google.maps.Marker({ I need to be able to set icon: '/img/a.png' or icon: '/img/b.png' depending on the value of locations[i][3]
所以我对 属性 的猜测是正确的:您可以使用条件运算符来做到这一点:
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3] == "Yes" ? '/img/a.png' : '/img/b.png'
});
...如果 locations[i][3] == "Yes"
为真,icon
属性 的值将为 '/img/a.png'
,否则为 '/img/b.png'
。