如何将if语句转换为三元运算符

时间:2018-05-15 16:50:17

标签: javascript ecmascript-6 ternary-operator

如何将以下javascript代码重写为ES6版本:

if(allUser[currentUser]['email']==email){
        if(allUser[currentUser]['password']==password){
            isUserFound = true
            passwordCorrect = true 
            break
        } else {
            isUserFound = true
            passwordCorrect = false
            break
        }
    } else {
        isUserFound = false
    }

1 个答案:

答案 0 :(得分:3)

这不是一个好主意,使用if / else,您的代码更具可读性。您可以简化为:

isUserFound = allUser[currentUser]['email'] === email;
passwordCorrect = isUserFound && allUser[currentUser]['password'] == password

如果您不知道,break也不会对ifforwhileswitch产生任何影响。