Javascript if数组元素条件内for循环

Javascript if数组元素条件内for循环,javascript,arrays,google-maps,google-maps-api-3,Javascript,Arrays,Google Maps,Google Maps Api 3,我在谷歌地图上绘制了一系列JavaScript数组中返回的位置 我试图根据一个数组元素的值更改标记图标的类型,如下所示 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]

我在谷歌地图上绘制了一系列JavaScript数组中返回的位置

我试图根据一个数组元素的值更改标记图标的类型,如下所示

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));
}
我错过了什么

我错过了什么

您将流代码放在对象初始化器的中间:

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));
}

…如果
位置[i][3]==“Yes”
为真,则
图标
属性的值将为
'/img/a.png'
,如果不是,则为
'/img/b.png'

是的,这确实有效,但我应该更具体一点:在新的google.maps.Marker中({我需要能够设置icon:'/img/a.png'或icon:'/img/b.png',具体取决于位置[I][3]的值。你可以像这样使用自执行函数,或者使用三元运算符。我将接受这一点,因为它更简洁
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));
}
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'
});