三元条件下的多个 OR 运算符,

Posted

技术标签:

【中文标题】三元条件下的多个 OR 运算符,【英文标题】:Multiple OR operators in ternary condition, 【发布时间】:2020-05-18 07:17:32 【问题描述】:

我正在尝试通过检查字符的值并根据三元运算符的真假评估来替换它来创建一个新字符串。

我只使用一个字符就成功了,从我读过的内容来看,可以有一个三元运算符条件包括 ||或运营商。我试过只使用两个,但没有产生正确的结果。

是不是因为条件一旦满足就不会过去了||还是运营商?

三元条件可以包含多少,将条件放入变量或函数会更好吗?

我知道问题可以通过不同的方式解决,但我正在尝试使用三元运算符以获得更好的理解。

提前致谢,我是 JavsScript 的新手。

.

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. citizens who shop online using Amazon.com, Walmart.com or other e-commerce websites, the White House said.'


const vowels = ['a', 'e', 'i', 'o', 'u']
const ranNum = () => return Math.floor(Math.random() * 5)

let news = ''

const swapper = input => 
  let count = 0
    while (count != input.length) 
      let cond = input[count].toLowerCase()
      cond != ' ' || cond != 'a' ? news += vowels[ranNum()] : news += input[count]
    count ++
   console.log(news)




console.log(input)
swapper(input)

//c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u'

【问题讨论】:

你不能像那样使用三元。它现在作为短路工作 【参考方案1】:

问题是

cond != ' ' || cond != 'a' ? (...)

这个条件总是为真 - 如果cond 是一个空格,它将满足cond != 'a'。如果cond'a',它将满足cond != ' '。如果cond 是别的,它将满足cond != ' '

改为使用:

(cond === ' ' || cond === 'a') ? news += input[count] : news += vowels[ranNum()];

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.'


const vowels = ['a', 'e', 'i', 'o', 'u']
const ranNum = () => return Math.floor(Math.random() * 5)

let news = ''

const swapper = input => 
  let count = 0
    while (count != input.length) 
      let cond = input[count].toLowerCase();
      (cond === ' ' || cond === 'a') ? news += input[count] : news += vowels[ranNum()];
    count ++
   console.log(news)




console.log(input)
swapper(input)

//c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u'

也就是说,你真的不应该滥用条件运算符来代替if-else

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.'


const vowels = ['a', 'e', 'i', 'o', 'u']
const ranNum = () => 
  return Math.floor(Math.random() * 5)


let news = ''

const swapper = input => 
  let count = 0
  while (count != input.length) 
    let cond = input[count].toLowerCase();
    if (cond === ' ' || cond === 'a') 
      news += input[count]
     else 
      news += vowels[ranNum()];
    
    count++
  
  console.log(news)




console.log(input)
swapper(input)

如果你想在这里使用条件运算符,你应该在news +=部分之后

news += (cond === ' ' || cond === 'a') ? input[count] : vowels[ranNum()];

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.'


const vowels = ['a', 'e', 'i', 'o', 'u']
const ranNum = () => 
  return Math.floor(Math.random() * 5)


let news = ''

const swapper = input => 
  let count = 0
  while (count != input.length) 
    let cond = input[count].toLowerCase();
    news += (cond === ' ' || cond === 'a') ? input[count] : vowels[ranNum()];
    count++
  
  console.log(news)




console.log(input)
swapper(input)

当有多个值要检查时,使用数组可能更清楚(尤其是如果您计划最终进行超过 2 次检查):

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.'


const vowels = ['a', 'e', 'i', 'o', 'u']
const ranNum = () => 
  return Math.floor(Math.random() * 5)


let news = ''

const swapper = input => 
  let count = 0
  while (count != input.length) 
    let cond = input[count].toLowerCase();
    news += [' ', 'a'].includes(cond) ? input[count] : vowels[ranNum()];
    count++
  
  console.log(news)




console.log(input)
swapper(input)

【讨论】:

当比较多个值时,我已经习惯使用这种“模式”:news += ([' ', 'a'].includes(cond)) ? input[count] : vowels[ranNum()];。使其更具可读性,易于添加/删除比较值,并且更简洁。如果是简单字母,您也可以使用字符串而不是数组:news += (' a'.includes(cond)) ? input[count] : vowels[ranNum()];

以上是关于三元条件下的多个 OR 运算符,的主要内容,如果未能解决你的问题,请参考以下文章

Angular ngClass 多个三元运算符条件

我可以在 [ngClass] 中使用条件(三元)运算符以及 Angular2 中的多个类条件吗?

试图弄清楚如何将具有多个条件的长三元运算符转换为长if语句

具有多个条件的JS三元函数?

在“for”循环条件中使用“三元运算”是一种好习惯吗?

Swift:SwiftUI 视图的多个条件的内联评估