在jQuery中使用多个记录ID过滤JSON数据 [英] Filter JSON Data with multiple record IDs in jQuery

查看:230
本文介绍了在jQuery中使用多个记录ID过滤JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Subhaze的名为filterScore的函数 ...只是工作正常,如果有单一的值需要传递给 KEY ...但我的问题是如果有多个值需要解析?



例如,有一个名为 brand_id 的键是整数。我想解析那些有 brand_id = 1或3或5或7 (多个)JQuery RegExp Filter 匹配 ...请帮助我这个家伙!



我该怎么做?



如果有人能帮我解决这个问题, >

解决方案

由于品牌ID是一个列表,而不是一个模式,您可以修改过滤器函数接受 brand_id 作为数组而不是 regex 。新的过滤器函数是

pre $函数filterStore(dataStore,filter){
return $(dataStore).filter(function (index,item){
for(var i in filter){
if(filter [i] instanceof Array){
if($。inArray(parseInt(item [i],10 ),filter [i])== -1)
return null;
else
continue;
}
if(!item [i] .toString() .match(filter [i]))return null;
}
return item;
});

$ / code>

然后你可以把所有的品牌id都放在这样的数组中

  var filter = {
brand_id:[1,2,3],
productname:new RegExp('(。*?)','gi'),
price:new RegExp('。*?','gi')
};

工作示例


Based on Subhaze's Function named "filterScore"... is just working fine if there is single value needs to be passed to KEY... But my question is what if there are multiple values needs to be parsed?

like for example there is a key named brand_id which is integer.. And I want to parse those data which have brand_id = 1 OR 3 OR 5 OR 7 (multiple) in JQuery RegExp, Filter, Match... Please help me out with this guyz!!

How can I do this??

Would really appreciate if anyone can help me with this

解决方案

As brands id are a list and not a pattern you can modify the filter function to accept brand_id as an array instead of a regex. New filter function would be

function filterStore(dataStore, filter) {
    return $(dataStore).filter(function(index, item) {
        for( var i in filter ) {
            if(filter[i] instanceof Array){   
              if($.inArray(parseInt(item[i],10),filter[i]) == -1)
                 return null;
              else
                 continue;                  
            }
           if( ! item[i].toString().match( filter[i] ) ) return null;
        }
        return item;
    });
}

Then you can put all your brand id's in a array like this

var filter = {
    "brand_id": [1,2,3],
    "productname": new RegExp('(.*?)', 'gi'),
    "price": new RegExp('.*?', 'gi')
};

Working Example

这篇关于在jQuery中使用多个记录ID过滤JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆