带if的Javascript字符串连接
为了提取条件值,我将如何执行以下操作带if的Javascript字符串连接,javascript,Javascript,为了提取条件值,我将如何执行以下操作 formatter: function() {return ' ' + '<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' + '<b>Volume: </b>' + if (this.y) {'Successful';} else { 'Failed';} + '<br /&g
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + if (this.y) {'Successful';} else { 'Failed';} + '<br />'
},},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+if(this.y){'Successful';}else{'Failed';}+'
'
},},
将if条件包装到一个函数中,在该函数中可以解析条件并返回正确的字符串
var "first part" + SomeMethodWithIfLogic(val) + "last part";
将if条件包装到一个函数中,在该函数中可以解析条件并返回正确的字符串
var "first part" + SomeMethodWithIfLogic(val) + "last part";
因此,代替您的if:
(this.y ? 'Successful' : 'Failed')
因此,代替您的if:
(this.y ? 'Successful' : 'Failed')
创建单独的变量以存储消息:
var message = 'Failed';
if (this.y) {
message = 'Successful';
}
或使用?:
运算符:
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + (this.y ? 'Successful' : 'Failed') + '<br />'
},},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+(this.y?'Successful':'Failed')+'
},},
创建单独的变量来存储消息:
var message = 'Failed';
if (this.y) {
message = 'Successful';
}
或使用?:
运算符:
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + (this.y ? 'Successful' : 'Failed') + '<br />'
},},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+(this.y?'Successful':'Failed')+'
},},
您需要使用三元运算符
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + (this.y? 'Successful' : 'Failed') + '<br />'
},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+(this.y?'Successful':'Failed')+'
},
您需要使用三元运算符
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + (this.y? 'Successful' : 'Failed') + '<br />'
},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+(this.y?'Successful':'Failed')+'
},
格式化程序:函数(){
如果(这个是y){
成功=‘成功’
}否则{
success='unsuccess'
};
返回“”+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+success+'
'
格式化程序:函数(){
如果(这个是y){
成功=‘成功’
}否则{
success='unsuccess'
};
返回“”+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
'卷:'+success+'
'
您可以执行以下操作:
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + ((this.y == true) ? "Successful": "Failed") + '<br />'
},},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
“卷:”+((this.y==true)“成功”:“失败”)+“
”
},},
该三元运算符将允许您在字符串中插入逻辑
细分:
如果此.y为真,则返回“成功”,否则返回“失败”
您可以执行以下操作:
formatter: function() {return ' ' +
'<b>Time: </b>' + Highcharts.dateFormat('%b %d, %H:%M ', this.x) + '<br />' +
'<b>Volume: </b>' + ((this.y == true) ? "Successful": "Failed") + '<br />'
},},
格式化程序:函数(){return''+
“时间:”+Highcharts.dateFormat(“%b%d,%H:%M”,this.x)+“
”+
“卷:”+((this.y==true)“成功”:“失败”)+“
”
},},
该三元运算符将允许您在字符串中插入逻辑
细分:
如果此.y为真,则返回“成功”,否则返回“失败”