简体   繁体   English

javascript“或”-它们可以组合成一个数组吗?

[英]javascript “ors” - can they be combined into an array?

wondering if I can make my javascript more efficient. 想知道我是否可以使我的JavaScript更有效率。 I have a var JSP = "the jsp's name" 我有一个var JSP =“ jsp的名称”

And I have statements in a javascript validation file: 我在javascript验证文件中有语句:

if(( JSP == "test.html" ) || ( JSP == "test1.html") || ( JSP == "test2.html" )) then blah blah blah.

Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

If you know that JSP contains a string, it's slightly more efficient to use === rather than == . 如果您知道JSP包含一个字符串,则使用===而不是==稍微更有效。 Also note that you don't need all those parens: 另请注意,您不需要所有这些括号:

if (JSP === "test.html" || JSP === "test1.html" || JSP === "test2.html") {
    // do something
}

You could also use a regular expression: 您还可以使用正则表达式:

if (/^test[12]?\.html$/.test(JSP)) {
    // do something
}

...but it depends what you mean by "efficient." ...但这取决于您所说的“有效”。 The series of === will be very efficient at runtime. ===系列将在运行时非常有效。

Separately, you could use a switch : 另外,您可以使用一个switch

switch (JSP) {
    case "test.html":
    case "test1.html":
    case "test2.html":
        // Do it
        break;
}

...but I wouldn't call it more efficient. ...但是我不会说它更有效率。

I definitely would not put the options in an array, because searching through the array will not be efficient. 我绝对不会将选项放在数组中,因为在数组中搜索效率不高。 But you can use a map: 但是您可以使用地图:

var pages = {
    "test.html":  true,
    "test1.html": true,
    "test2.html": true
};

...and then this test: ...然后进行此测试:

if (pages[JSP] === true) {
    // do something
}

...which results in a fairly efficient property lookup. ...这会导致相当有效的属性查找。 That's only reasonable if you create the object once and reuse it. 仅当您一次创建对象并重用它时,这才是合理的。

(You might have people say "Or just use if (pages[JSP]) { ... } . But that fails if JSP happens to contain "toString" or "valueOf" or any of several other inherited properties blank objects get from Object.prototype . It's fine if you're certain it won't have any of those values, though.) (您可能会说“或者只使用if (pages[JSP]) { ... } 。但是,如果JSP碰巧包含“ toString”或“ valueOf”或空白对象从Object.prototype获得的任何其他继承的属性中的任何一个,则此操作将失败。 Object.prototype 。如果您确定它不会包含任何这些值,那就很好了。)

You could create an object with those keys: 您可以使用这些键创建一个对象:

var theThings = { "test.html": true, "test1.html": true, "test2.html": true };

if (theThings[JSP]) { /* whatever */ }

If there are only three or four, it might not be worth it, but if there are dozens it'd definitely be faster, especially if the test gets made several times. 如果只有三个或四个,那可能不值得,但是如果有几十个,那肯定会更快,特别是如果测试多次进行。

edit — wow I'm crying a little inside here, guys. 编辑 -哇,伙计们,我在这里哭了一点。 Property name lookups are going to be way faster than linear searches through an array. 物业名称查找会比通过阵列线性搜索方式更快。

var arr = ['test.html', 'test1.html', 'test2.html'];
if (arr.indexOf(JSP)) != -1) {
   alert("found it!");
}

relevant docs here . 相关文档在这里

if( JSP in {"test.html":0, "test2.html":0, "test3.html":0} ) {
...
}

It doesn't get any closer to SQL's IN( 1, 2, 3) than this in javascript :-) 它没有比javascript中的SQL更接近SQL的IN( 1, 2, 3) :-)

if (["test.html", "test1.html", "test2.html"].indexOf(JSP) > -1)

对于不支持数组上的indexOf浏览器, MDC建议使用短代码来添加缺少的功能。

Probably not more efficient, but you got cleaner ways to do it. 可能效率不高,但是您有更清洁的方法来执行此操作。 You could for instance use a switch-case like this: 例如,您可以使用像这样的开关盒:

switch(JSP) {
    case 'test.html':
    case 'test1.html':
    case 'test2.html':
    blablabla; break;
}

Or you could create an array out of the urls and see if your string is in the array like this 或者,您可以根据网址创建一个数组,然后查看字符串是否在数组中,如下所示

var arr = [
    'test.html', 'test1.html',
    'test2.html'
];
if(arr.indexOf(JSP) != -1) { blablabla; }

The last one will not work in all browsers. 最后一个并非在所有浏览器中都适用。

A way of doing it in jQuery is to use the inArray method, eg: 在jQuery中执行此操作的一种方法是使用inArray方法,例如:

if ($.inArray(JSP, ["test.html", "test1.html", "test2.html"]) > -1) {
    // your code
}

The inArray method works in a similar manner to String.indexOf so -1 is returned if no match. inArray方法的工作方式与String.indexOf相似,因此如果不匹配,则返回-1

Use a regular expression? 使用正则表达式?

if (/^test\d?\.html$/.test(JSP)) { ... }

I can't promise that will be more efficient though, just tidier code-wise. 我不能保证这样做会更高效,只是在代码方面更为合理。

Or if you're already using jQuery, you could use jQuery.inArray(): 或者,如果您已经在使用jQuery,则可以使用jQuery.inArray():

var names = ['test.html', 'test2.html', 'test3.html'];
if ($.inArray(JSP, names)) { ... }

Or with underscore.js 或使用underscore.js

var names = ['test.html', 'test2.html', 'test3.html'];
if (_.indexOf(names, JSP) !== -1) { ... }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM