Filter JSON Data with multiple record IDs in jQuer

2019-05-29 13:07发布

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

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-29 13:33

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

查看更多
登录 后发表回答