Javascript:将一个值与多个值进行比较的最漂亮的方法 [英] Javascript: The prettiest way to compare one value against multiple values
本文介绍了Javascript:将一个值与多个值进行比较的最漂亮的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道有很多方法可以做到这一点,
I know there are loads of ways of doing this, but I'm looking for the neatest.
我问,因为我希望这是可行的(它不是,很显然,当你看看它):
i ask because i'd hoped this was workable (it isn't, quite obviously when you look at it):
if (foobar == (foo||bar) ) {
//do something
}
推荐答案
不要太鬼祟,特别是当它不必要地影响性能。
如果你真的有一堆比较做,只是格式化。
Don't try to be too sneaky, especially when it needlessly affects performance. If you really have a whole heap of comparisons to do, just format it nicely.
if (foobar === foo ||
foobar === bar ||
foobar === baz ||
foobar === pew) {
//do something
}
这篇关于Javascript:将一个值与多个值进行比较的最漂亮的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文