我有一个const numberOfComments是数组的大小,const riskByComments如果numberOfComments要大于2,我想拥有另一个值为5的值,否则应为0。
我知道我可以使用轻松地做到这一点let,但是我知道最好只使用const
export const calculateRisk = (ticket) => dispatch => {
const numberOfComments = ticket.comments.length
let riskByComments//if i use const i need to declare its value
//right away, i cannot use an if statement to do it later
if(ticket.comments.length>2){
riskByComments=5//if i use const inside the if statement
}else{
riskByComments=0//i cannot use it in the rest of my function
}
Run Code Online (Sandbox Code Playgroud)
使用条件运算符:
const riskByComments = numberOfComments >2 ? 5 : 0;
Run Code Online (Sandbox Code Playgroud)