1

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 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Joy!!! It worked for me.. But what happens.. When we match for brand_id 1,2 or 3... it also returns records contains 11, 12, or 13 or even 33, 22, 11.. as it matches 1,2 or 3 in key named "brand_id".. how to solve this??
You can better pass an array for brand_id as you have a set of values to check for rather than a pattern, so I have modified the filter function where you can pass an array for brand_id, check here jsfiddle.net/joycse06/bsaJm/4
Hey Joy... Thanks a lot man.. you made my day.. :) But would like to ask if there is any other option without looping through all the records?? as it takes long time to process for large amount of records..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.