简体   繁体   English

有什么办法可以使我的jquery搜索更好?

[英]is there any way to make my jquery search better?

var myarr= Array('test1','test2','test3'); var myarr = Array('test1','test2','test3');

var searchTerm = "test";
var rSearchTerm = new RegExp( searchTerm,'i');

$.each(myarr, function(i) {
        if (myarr[i].match(rSearchTerm)) {
            //item found
        }

    });​

guys is there any way to make my search algorithm better ? 你们有什么方法可以使我的搜索算法更好? "myarr" will be a big array so i want to make sure that i'm using the best way to search in it “ myarr”将是一个很大的数组,因此我想确保我使用的是最佳搜索方式

thanks alot 非常感谢

I would recommend the following (since jQuery provides this convenience): 我建议以下内容(因为jQuery提供了这种便利):

$.each(myarr, function(index, value) {
    if (rSearchTerm.test(value)) {
        // item found
    }
});

The only other approach to make this faster is probably to do this without jQuery in a plain for -loop, since it does not involve callbacks: 使此速度更快的唯一其他方法可能是在没有jQuery的情况下简单地for -loop进行此操作,因为它不涉及回调:

for (var i = 0; i < myarr.length; i++) {
    if (rSearchTerm.test(myarr[i])) {
        // item found
    }
}

EDIT: I changed .match() to .test() , like Andy E suggested. 编辑:我将.match()更改为.test() ,就像Andy E建议的那样。

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

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