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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:49:50+00:00 2026-05-22T18:49:50+00:00

Given the following data structure var things = [{ name: thing1, sex: male}, {

  • 0

Given the following data structure

var things = [{ "name": "thing1", "sex": "male"},
              { "name": "thing2", "sex": "female"}];

I would like to be able to search that array of objects and pull out a particular object.

I currently have the following code written

function selectElementByName (name) { 

  var returnObject;

  for (var i = 0; i < things.length; i++) {

    if (things[i].name === name) {

        returnObject = things[i];
    }
  } 


  if ( returnObject === undefined) { 
    console.log("Object not found");
  }

  return returnObject;
}

JsFiddle can be found here

Is there a more efficient way of doing this?

  • 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-22T18:49:51+00:00Added an answer on May 22, 2026 at 6:49 pm

    You can make an early exit when an object is found, so that you don’t have to loop through the rest of the array:

    function selectElementByName(name) { 
      for (var i = 0; i < things.length; i++) {
        if (things[i].name === name) {
          return things[i];
        }
      }
      console.log("Object not found");
    }
    

    (This will however change the behaviour if there are duplicates, so that it returns the first object found instead of the last.)


    If the names are unique, you could use them as key and store the objects in an object instead of in an array:

    var things = {
      "thing1": { "name": "thing1", "sex": "male"},
      "thing2": { "name": "thing2", "sex": "female"}
    };
    

    Then you wouldn’t need to loop to find an object:

    function selectElementByName(name) { 
      return things[name];
    }
    

    If you need the objects in an array, you could still create an index for searching if the array doesn’t change so often:

    var thingsNameIndex = {};
    for (var i = 0; i < things.length; i++) {
      thingsNameIndex[things[i].name] = i;
    }
    

    Now you can use the index to find the object:

    function selectElementByName(name) { 
      return things[thingsNameIndex[name]];
    }
    

    As you have to update or recreate the index as soon as the array changes, this would only be usedful if you do searches in the array much more often than you change the array.

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

Sidebar

Related Questions

Given the following JavaScript data structure - how would you describe it? BLA =
I have an odd linq subquery issue. Given the following data structure: Parents Children
Given the following tree structure in Haskell: data Tree = Leaf Int | Node
Given the following schema / data / output how would I format a SQL
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
I was asked the following Question: How would you store the data given below(which
Given is the following data frame: structure(list(UH6401 = c(1, 1, 0, 0, 0, 1,
I'd like to implement the following data structure in c++ (pseudo code): Map<Integer, Integer>
Let's say I have a data structure like the following: Camera { double x,
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE

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.