如何使用具有多个值的逻辑或运算符?
使用多个 or 运算符时如何简化代码。 我有一个从 0 到 6 的数字列表,用逻辑或分隔。有什么方法可以简化它吗?
if (filteredMnth === 'mnth') {
return (new Date(exp?.date).getMonth().toString() === "0" || "1" || "2" || "3" || "4" || "5" || "6" )
}
How to simplify the code when using multiple or operators.
I have a list of numbers from 0 to 6 separated by logical or.Is there any way to simplify it?
if (filteredMnth === 'mnth') {
return (new Date(exp?.date).getMonth().toString() === "0" || "1" || "2" || "3" || "4" || "5" || "6" )
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这种具体情况,您可以执行 ->
For this specific case you can do ->
一个很好的模式是创建一个
有效
或接受
值的数组,并使用Array.prototype.includes
检查用户输入中是否有一个:As @罗比Cornelissen 已经在评论中提到,在你的情况下它确实没有任何意义,但我在此处包含此模式是为了回答你的问题的更通用版本。
A nice pattern for this, is to create an Array of
valid
oraccepted
values and use theArray.prototype.includes
to check for one from the user input:As @Robby Cornelissen already mentioned in the comment, in your case it really does not make any sense, but I'm including this pattern here to answer a more generic version of your question.
由于您有多个值,因此可以使用列表,并包含下面的方法
Since you are having multiple values you can make use of List and includes method as below