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 6619513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:45+00:00 2026-05-25T20:58:45+00:00

Say I have an object: userInfo And I want to search each node of

  • 0

Say I have an object:

userInfo

And I want to search each node of userInfo to see if the key ‘username’ has a value equal to foo.

userInfo[x].username == "foo" 

Is there a better way of doing the following?

var matchFound = false;

for (var i = 0, len = userInfo.length; i < len; i++)
     matchFound = userInfo[i].username == "foo";
  • 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-25T20:58:45+00:00Added an answer on May 25, 2026 at 8:58 pm

    There isn’t really a better (more efficient) way without introducing another data structure. The answer really depends on your usage but you could do a few different things:

    1. Create separate ‘indexes’ using hashes. These structures would map keys to the items or the index in the source array. JavaScript objects/hashes support key based lookup and should be efficient.

      userinfo[x].username = "foo";
      // Index the objects
      usersByName = {};
      usersByName["foo"] = userinfo[x];
      // -- OR -- index the array indices
      var usersByName["foo"] = x;
      // Test for key
      "foo" in usersByName; // true
      

      You’ll have to put in a little more work to maintain consistency between the index and the source array. It’s probably best to wrap both in another object to manage the contents of both. This approach is nice if there are multiple fields that you want to look objects up by.

    2. If you don’t care about the order of the collection you could just change the whole thing to a hash and index by username

      var userinfo = {};
      userinfo["foo"] = {username: "foo", firstName: "Foo", lastName: "Bar"};
      

    One thing to think about, though, is if the efficiency gains are going to outweigh the increased code complexity of maintaining indexes. If you aren’t doing a lot of searches and you don’t have tons of items in the userinfo collection it may make more sense to just write a general use searching function or use a library like what Philip Schweiger was mentioning.

    function findObjectByAttribute (items, attribute, value) {
      for (var i = 0; i < items.length; i++) {
        if (items[i][attribute] === value) {
          return items[i];
        }
      }
      return null;
    }
    var userinfo = [];
    userinfo[0] = {username: "foo"};
    console.log(findObjectByAttribute(userinfo, "username", "foo"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an object var my_obj = new Object(); my_obj['key'] = value; Is
Say i have this object: test = { testObj: { 1: { key: value
Say I have an object containing a value. I wish to get the index
Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
Lets say I have an object that has stringProp1, stringProp2. I wish to store
Let's say I have an object named foo with another object named bar as
Say I have Object A which has a member of type Object B.. and
Lets say I have object Tom which has class Person. Class Person String Name
Let's say I have: object A, B, C each with corresponding models, views, and
Say you have an object A with property B and you want to define

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.