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

  • SEARCH
  • Home
  • 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 6941655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:54:49+00:00 2026-05-27T12:54:49+00:00

I have an array of homogeneous objects like so; [ { foo : bar,

  • 0

I have an array of homogeneous objects like so;

[
  {
    "foo" : "bar",
    "bar" : "sit"
  },
  {
    "foo" : "lorem",
    "bar" : "ipsum"
  },
  {
    "foo" : "dolor",
    "bar" : "amet"
  }
]

I’d like to search these objects’ values (not keys) with a keyword, and return an array of objects that contain the keyword in any of the values.

So for example, with a keyword r, I would get all the objects (“baR” in object #1, “loRem” in object #2 and “doloR” in object #3). With a keyword lo, I would get objects 2 and 3 (“LOrem” and “doLOr”), with a, I’d get objects 1 and 3, (“bAr” and “Amet”). With the keyword foo however, I would get an empty array, since “foo” is a key, and isn’t found in any of the values (unlike “bar”)… you get the idea.

How would I go about doing this? Thanks a lot in advance!

  • 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-27T12:54:50+00:00Added an answer on May 27, 2026 at 12:54 pm

    Something like this:

    var objects = [
      {
        "foo" : "bar",
        "bar" : "sit"
      },
      {
        "foo" : "lorem",
        "bar" : "ipsum"
      },
      {
        "foo" : "dolor",
        "bar" : "amet"
      }
    ];
    
    var results = [];
    
    var toSearch = "lo";
    
    for(var i=0; i<objects.length; i++) {
      for(key in objects[i]) {
        if(objects[i][key].indexOf(toSearch)!=-1) {
          results.push(objects[i]);
        }
      }
    }
    

    The results array will contain all matched objects.

    If you search for ‘lo’, the result will be like:

    [{ foo="lorem", bar="ipsum"}, { foo="dolor", bar="amet"}]
    

    NEW VERSION – Added trim code, code to ensure no duplicates in result set.

    function trimString(s) {
      var l=0, r=s.length -1;
      while(l < s.length && s[l] == ' ') l++;
      while(r > l && s[r] == ' ') r-=1;
      return s.substring(l, r+1);
    }
    
    function compareObjects(o1, o2) {
      var k = '';
      for(k in o1) if(o1[k] != o2[k]) return false;
      for(k in o2) if(o1[k] != o2[k]) return false;
      return true;
    }
    
    function itemExists(haystack, needle) {
      for(var i=0; i<haystack.length; i++) if(compareObjects(haystack[i], needle)) return true;
      return false;
    }
    
    var objects = [
      {
        "foo" : "bar",
        "bar" : "sit"
      },
      {
        "foo" : "lorem",
        "bar" : "ipsum"
      },
      {
        "foo" : "dolor blor",
        "bar" : "amet blo"
      }
    ];
    
    function searchFor(toSearch) {
      var results = [];
      toSearch = trimString(toSearch); // trim it
      for(var i=0; i<objects.length; i++) {
        for(var key in objects[i]) {
          if(objects[i][key].indexOf(toSearch)!=-1) {
            if(!itemExists(results, objects[i])) results.push(objects[i]);
          }
        }
      }
      return results;
    }
    
    console.log(searchFor('lo '));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why, if I have array of objects like this: class testClass { private $_x
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
An array is defined of assumed elements like I have array like String[] strArray
Have an array of chars like char members[255]. How can I empty it completely
I have array like below, Array ( [14289] => Array ( [0] => Karthikeyan
so I have array like ParamsArray {a,b,a,a,...b} (so i have 2 kinds of parameters
I have array and contains 50 values , example [ 'london', 'bluehit', 'green', 'lonpark',
I have array of objects with created images (Object.Image), i want to show images
I have array of keywords. How can I find if any of these keywords
I have array of objects. I want to write method wich i will use

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.