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
}
});
大家有什么办法可以让我的搜索算法变得更好吗? “myarr”将是一个大数组,所以我想确保我使用最好的方式在其中搜索
非常感谢
最佳答案
我会推荐以下内容(因为 jQuery 提供了这种便利):
$.each(myarr, function(index, value) {
if (rSearchTerm.test(value)) {
// item found
}
});
唯一加快速度的其他方法可能是在普通的 for
循环中不使用 jQuery 来完成此操作,因为它不涉及回调:
for (var i = 0; i < myarr.length; i++) {
if (rSearchTerm.test(myarr[i])) {
// item found
}
}
编辑:我将.match()
更改为.test()
,就像Andy E建议的那样。
关于javascript - 有什么方法可以让我的 jquery 搜索更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3657089/