Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:47:36+00:00 2026-05-28T07:47:36+00:00

I want to filter and search through Json using multiple keywords. I have it

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:47:37+00:00Added an answer on May 28, 2026 at 7:47 am

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to filter DFWP through asp:TextBox. Using Office Designer I added DFWP with
I have a json object array. I want to search the array and for
For a phrase search, we want to bring up results only if there's an
For a phrase search, we want to bring up results only if there's an
I want to filter based on an attribute called level. Where I have written
i want to filter a String by using the \w wildcard, but unfortunately it
I want to search for a user's email by using Active Directory. Available is
I am using meta_search to filter through a listing of cars. The car model
I want to filter my result set before I search. I know the correct
I am using the AdvancedDatabaseCrawler as a base for my search page. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.