有没有什么干净的方法可以对一组||测试进行编码,看看变量的值是否在其中一个测试中

Is there any clean way to code a group of || testing to see if the value of a variable is in one of them?

本文关键字:测试 编码 变量 一个 在其中 是否 一组 方法 什么 有没有      更新时间:2023-12-13

我有这样的代码:

if (view == 'delete' || view =='edit' || view == 'new') {} 

有没有一种更干净的方法可以在不使用外部库的情况下编写代码。只是看起来不太好,有所有的||,也许以后更多。

使用indexOf(在古代IE版本上不起作用):

if (["delete", "edit", "new"].indexOf( view ) >= 0) { alert("ME");}

您也可以使用正则表达式:

if (/^(delete|edit|new)$/.test(view)) {
    // true
}