2

I want to filter and search through Json using multiple keywords. I have it working when there is only one value to check against, but I need to use multiple keyword search filters.

In this example, I want to return the objects related to Wal-mart and China but NOT Wal-Mart in the US or Target in China.

I create an array of keywords to filter using Grep() and Map that out in console. I cannot figure out the syntax to check the value against the keyword however (see my comment below, "This is not right.")

<!DOCTYPE html>

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>

var keywords = ["China", "Wal-Mart"];//filter Json by these terms

var data = [
    {
    "name": "Store #1",
    "country": "China",
    "retailer": "Wal-Mart"
    },
    {
    "name": "New Store #2",
    "country": "China",
    "retailer": "Wal-Mart"
    },
    {
    "name": "Store Name US",
    "country": "United States",
    "retailer": "Wal-Mart"
    },
    {
    "name": "Target #98237",
    "country": "China",
    "retailer": "Target"
    }
]
var searchkeywords = $.grep(data, function(value,i) { 

return value[i] === keywords[i];//<--- This is not right

//should be the equivalent of above keywords:
//return value.country.toLowerCase() === "china" && value.retailer.toLowerCase() === "wal-mart" 
});

var keywordsearchresult = $.map(searchkeywords, function(value, i) {
    return {
        name: value.name,
        country: value.country,
        retailer: value.retailer
    };
});
console.log(keywordsearchresult);//should return first 2 items
</script>
</head>

<body>
</body>
</html>

In my application, there will be more keywords and items (type, date, etc..) in the Json, this is just a simplified version. I basically want to add a lot of rules and conditions to the Grep.

1 Answer 1

2

I think your main problem is that your search terms give no indication of which field they are supposed to be filtering on. I would change the format of your terms to this:

var keywords = [
    {
        field: "country",
        value: "China"
    },
    {
        field: "retailer",
        value: "Wal-Mart"
    }
]; 

This allows your search to search the appropriate fields for each word. Then you can change your grep to this:

var results = $.grep(data, function(value, i) {
    for(var i=0;i<keywords.length;i++){
        if(value[keywords[i].field] !== keywords[i].value){
            return false   
        }
    }

    return true;        
});

I have removed the map part because I can't see any use for it. Then you are left with this:

http://jsfiddle.net/kZWef/

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

5 Comments

Thanks! I was using map to render a html template. But I can instead just use results as a var object to populate instead.
How would I search on two keywords using country? For example if I wanted to return where country = United States OR China var keywords = [ { field: "country", value: "China" }, { field: "country", value: "United States" } ];
You could makes the value an array {field: "country", value: ["China", "United States"]}.
Yes, you would have to loop through the value array. But you could not simply return false if one doesn't match, you would have to check that the value does not match any of the values, then return false.
Actually, if you're using jquery you can just use inArray. api.jquery.com/jQuery.inArray

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.