javascript - 如何用三元运算输出三个结果的值

标签 javascript

我正在遍历一组数组。每个元素具有三个值。第三个值可以是 HighMedLow

我要输出

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/

相关文章:

javascript - 如何使用 AngularJS 重新加载或重新渲染整个页面

javascript - jQuery改变嵌套在div、段落标签和强标签中的文本

javascript - AngularJS 工厂 getter 和 setter 对象

javascript - 更改选择时未定义 JQuery 数据属性

javascript - 悬停时启动轮播

javascript - 加载外部js文件后在脚本元素中执行代码

javascript - ReactJS ReactCSSTransitionGroup 仅在初始加载时设置动画

javascript - 为什么使用 require './foldername/filename' 而不是 require 'foldername/filename' ?

javascript - 如何正确格式化从 webservice c# 返回的日期

javascript - 使用 'this' 引用与meteor.template.rendered