使用jQuery的grep使用通配符的对象数组搜索 [英] Searching array of objects using jquery grep with wildcards
本文介绍了使用jQuery的grep使用通配符的对象数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在寻找使用jQuery的grep对象的数组,并想在搜索中包含通配符。例如,我有一个数组如下:
I'm searching an array of objects using jquery grep and would like to include wildcards in the search. For example, I have an array as follows:
courses = [
{code: 'ENCH3TH', otherFields: otherStuff},
{code: 'ENCH3THHS1', otherFields: otherStuff},
{code: 'ENCH3TH2', otherFields: otherStuff},
{code: 'ENCH4RT', otherFields: otherStuff},
{code: 'ENCH4MT', otherFields: otherStuff}]
我想获得所有与ENCH3TH preFIX课程。我试图
I'd like to get all the courses with the ENCH3TH prefix. I have attempted
var resultSet = $.grep(courses, function(e){ return e.code == 'ENCH3TH/'; });
..无果(注意'ENCH3TH作为通配符使用后的'/')
..to no avail (note the use of the '/' after 'ENCH3TH' as the wildcard).
推荐答案
您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf\"相对=nofollow> String.indexOf()这里, =
将不与野生字符工作
You can use String.indexOf() here, =
will not work with wild chars
var resultSet = $.grep(courses, function (e) {
return e.code.indexOf('ENCH3TH') == 0;
});
演示:小提琴
或者使用正则表达式
var regex = /^ENCH3TH/
var resultSet = $.grep(courses, function (e) {
return regex.test(e.code);
});
演示:小提琴
这篇关于使用jQuery的grep使用通配符的对象数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文