Filter JSON Data with multiple record IDs in jQuery

▼魔方 西西 提交于 2020-01-14 04:39:08

问题


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:


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



来源:https://stackoverflow.com/questions/10870334/filter-json-data-with-multiple-record-ids-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!