我正在遍历一组数组。每个元素具有三个值。第三个值可以是 High
、Med
或 Low
我要输出
http://maps.google.com/mapfiles/ms/icons/red-dot.png for High
http://maps.google.com/mapfiles/ms/icons/yellow-dot.png for Med
http://maps.google.com/mapfiles/ms/icons/green-dot.png for Low
这是我所拥有的,当值为 High
(输出红色)或输出绿色时,它工作得很好。
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][0], markers[i][1]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
icon: markers[i][2] === 'High'? 'http://maps.google.com/mapfiles/ms/icons/red-dot.png' : 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
map: map,
title: "",
});
}
问题
我怎样才能像上面描述的那样为 High, Med, Low
工作?
最佳答案
你可以使用一个对象:
var icons = {
"High": "http://maps.google.com/mapfiles/ms/icons/red-dot.png",
"Med": "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png",
"Low": "http://maps.google.com/mapfiles/ms/icons/green-dot.png",
};
然后使用 icon: icons[markers[i][2]]
或类似的东西。
关于javascript - 如何用三元运算输出三个结果的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31414086/