【问题标题】:What's the shortest way to check if a variable is not equal to several other variables? [duplicate]检查一个变量是否不等于其他几个变量的最短方法是什么? [复制]
【发布时间】:2021-04-15 15:09:36
【问题描述】:

我想缩写这个:

data.urlPath !== '#profile' && data.urlPath !== '#communities' && data.urlPath !== '#other'

类似于:

data.urlPath !== '#profile' || '#communities' || '#other'
// this doesn't work

这可能吗?

【问题讨论】:

  • 我愿意['#profile' ,' #other', '#...'].includes(data.urlPath) ? action : otherAction

标签: javascript comparison-operators


【解决方案1】:

试试这个,

['#profile', '#communities','#other'].includes(data.urlPath)

【讨论】:

  • 上述原因条件仍然需要被否定才能符合OP的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2016-03-11
  • 2012-08-20
  • 2020-01-16
  • 2013-08-15
  • 2012-08-25
相关资源
最近更新 更多