简体   繁体   English

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

[英]how to convert multiple else if statements to ternary operator

How would i convert this if/else statement into a ternary operator instead of using if/else statements. 我如何将这个if/else语句转换为ternary operator而不是使用if / else语句。 I know this is simple to most javascript developers, but its confusing to me. 我知道这对大多数javascript开发人员来说都很简单,但对我来说却令人困惑。

  if(this.props.usersWhoAreTyping.length === 0) {
        return null
  } else if(this.props.usersWhoAreTyping.length === 1 ){
      return <p>{this.props.usersWhoAreTyping[0]} is typing...</p>
  }
   else if(this.props.usersWhoAreTyping.length > 1){
        return <p>{this.props.usersWhoAreTyping.join('and')} are typing...</p>
  }

My attempt was something like 我的尝试就像

 {this.props.usersWhoAreTyping.length === 0 ?  (
          null
 ): (
     <p>{this.props.usersWhoAreTyping[0]} is typing...</p>
 )}
 // not sure how to include the 2nd else if statement in this ternary operator 

I'd recommend sticking to the if/else, as nested ternaries can be confusing to people. 我建议您坚持使用if / else,因为嵌套的三进制可能会使人们感到困惑。 But here's how you'd do it: 但是,这是您的处理方式:

{this.props.usersWhoAreTyping.length === 0 ? (
  null
) : this.props.usersWhoAreTyping.length === 1 ? (
  <p>{this.props.usersWhoAreTyping[0]} is typing...</p>
) : (
  <p>{this.props.usersWhoAreTyping.join('and')} are typing...</p>
)}

Note: i did make the assumption that the 3 cases cover all possibilities. 注意:我确实假设这3种情况涵盖了所有可能性。 Ie, length can't be less than 0 or be a fraction. 即,长度不能小于0或为分数。 Since we're presumably dealing with an array, that seemed reasonable, and it let me drop the length > 1 check. 由于我们大概是在处理数组,所以这似乎很合理,它让我放弃了长度大于1的检查。

Using a ternary operator is simple just this: 使用三元运算符很简单:

  const result =
    this.props.usersWhoAreTyping.length > 0 ? (
      this.props.usersWhoAreTyping.length === 1 ? (
        <p>{this.props.usersWhoAreTyping[0]} is typing...</p>
      ) : (
        <p>{this.props.usersWhoAreTyping.join('and')} are typing...</p>
      )
    ) : null;

Take into account I modified the conditions due to get the case less than 0 考虑到我修改条件得不到0的情况

If I understand your intentions correctly, there is no need for a ternary. 如果我正确理解了您的意图,则无需三元。

Example: 例:

render() {
const { usersWhoAreTyping } = this.props

return (
   <div>
    {usersWhoAreTyping.length === 1 && <p>{usersWhoAreTyping[0]} is typing...</p>}
    {usersWhoAreTyping.length > 1 && <p>{usersWhoAreTyping.join('and')} are typing...</p>}
   </div>
)

 switch (this.props.usersWhoAreTyping.length) { case 0: { return null } case 1: { return <p > { this.props.usersWhoAreTyping[0] } is typing... < /p> } case 2: { return <p > { this.props.usersWhoAreTyping.join('and') } are typing... < /p> } default: return null } 

Try this if its okay for you. 如果适合您,请尝试此操作。

As mentioned above nested ternary operators can be confusing and should be avoided. 如上所述,嵌套三元运算符可能会造成混淆,应避免使用。 But if you really want to write code without multiple if/else, you can refer this. 但是,如果您确实想编写没有多个if / else的代码,则可以参考此内容。 It uses some ES6 features. 它使用了一些ES6功能。

const { usersWhoAreTyping } = this.props;

someFunc = () => {
    if(usersWhoAreTyping && usersWhoAreTyping.length) {
        const translation = usersWhoAreTyping.length === 0 ? 'is' : 'are';
        return <p>{ `${usersWhoAreTyping.join('and')} ${translation} typing...` }</p>
    }
    return null;
}

So, technically you could do what you're asking like this: 因此,从技术上讲,您可以按照以下要求进行操作:

 function showTypers(aArrayOfNames) { var iArrayLength = aArrayOfNames.length; return (iArrayLength === 0) ? null : '<p>' + ((iArrayLength > 1) ? aArrayOfNames.join(' and ') + ' are' : aArrayOfNames[0] + ' is') + ' typing...</p>'; } console.log(showTypers([])); console.log(showTypers(['Tom'])); console.log(showTypers(['Tom', 'Dick', 'Harry'])); 

That being said, I honestly wouldn't recommend nesting this particular scenario, specifically because one of the outcomes ( length === 0 ) results in a completely different outcome than the other two (amongst other reasons . . . nested ternary logic can be very confusing). 话虽这么说,老实说,我不建议嵌套这种特殊情况,特别是因为其中一个结果( length === 0 )导致的结果与其他两个结果完全不同(其他原因……嵌套三元逻辑可以是非常混淆)。 I'd recommend something more along these lines like this: 我会推荐一些类似的方法:

 function showTypers(aArrayOfNames) { var iArrayLength = aArrayOfNames.length; var sUserNames = ""; if (iArrayLength > 0) { sUserNames = (iArrayLength > 1) ? aArrayOfNames.join(' and ') + ' are' : aArrayOfNames[0] + ' is' ; } else { return null; } return '<p>' + sUserNames + ' typing...</p>'; } console.log(showTypers([])); console.log(showTypers(['Tom'])); console.log(showTypers(['Tom', 'Dick', 'Harry'])); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM