多个三元运算符 [英] Multiple Ternary Operators
问题描述
我需要一个三元运算符的语法帮助,这将帮助我将正确的标记图标放到我的好地图上。我有三个区域0,1和2,它们具有唯一的图标0,1和2。 / p>
我曾经只有两个区域,所以这个三元运算符工作正常;
icon :(区域== 1)? icon1:icon0,
现在我需要为area2添加一个额外的第三个图标(icon2)。
我尝试了各种方法,但似乎无法做到正确。
语法如下:
icon :( area == 1)? icon1 :(区域== 2)? icon2:icon0,
但这开始变得复杂。你可能最好只创建一个函数来代替这个工作:
icon:getIcon(area),
...
函数getIcon(区域){
if(area == 1){
return icon1;
}否则if(area == 2){
return icon2;
}
返回icon0;
}
I need a bit of syntax help with a ternary operator which will help me to put the correct marker icons on to my good map.I have three areas 0,1 and 2 which have unique icons 0, 1 and 2.
I used to have just two areas so this ternary operator worked fine;
icon: (area == 1) ? icon1: icon0,
Now I need to add an additional third icon (icon2) for area2.
I've tried various methods but just can't seem to get it right.
The syntax would be:
icon: (area == 1) ? icon1 : (area == 2) ? icon2 : icon0,
But this is starting to get complicated. You may well be better off just creating a function to do this work instead:
icon: getIcon(area),
...
function getIcon(area) {
if (area == 1) {
return icon1;
} else if (area == 2) {
return icon2;
}
return icon0;
}
这篇关于多个三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!