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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:47:10+00:00 2026-06-17T02:47:10+00:00

There’s a gap in my JavaScript knowledge here. I want to search an array

  • 0

There’s a gap in my JavaScript knowledge here. I want to search an array of objects values for a particular value and return it.

For the year I have been writing JavaScript, I have been implementing it like this:

var itemClicked = (function(){

  var retval;

  //Note self.inventory.itemsArray is an array of JS objects

  $(self.inventory.itemsArray).each(function(i){
    if(parseInt(this.id) === parseInt(idOfItem)){
      retval = this;
      return false;
    }
  });

  return retval;

})();

It works, but I’m sure as anything there is a more elegant way. Tell me please!

EDIT – Solution

Thanks to @gdoron with his answer below.

var myVar = $(self.owner.itemsArray).filter(function(){
   return parseInt(this.id) == parseInt(recItemID);
}).get(0);

Note: .get(0) was added at the end because myVar is wrapped as a jQuery object.

  • 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-06-17T02:47:11+00:00Added an answer on June 17, 2026 at 2:47 am

    The native jQuery function for this is filter:

    $(data).filter(function(){
        return this.id == "foo";
    });
    

    It’s shorter than code you have and more important a lot more readable.
    About efficiency, it will iterate all the elements in the set to find as much as possible matches, but I hardly believe it will be the bottle neck of your application, don’t focus on micro-optimisations.

    I suggest you read Eric Lipper blog about Which is faster.

    You can also use grep as suggested by @Mattias Buelens:

    $.grep(data, function(ele){
        retun ele.id == "foo";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a variable, suppose item . It is an array holding two values
There are 3 methods as f1, f2 and f3. I want to return from
There's a two year old blog post here explaining how to fix the error
There are two kinds equals methods? public boolean equals(Bigram b) { return b.first ==
There is a QListWidget containing keywords for search and a QTextEdit containing some text.
There was a thread on this in comp.lang.javascript recently where victory was announced but
There are some methods to solve the clearfix. I want to know if any
There's obviously something I'm missing here - I've been through the developer.android reference and
There are a few ways to do this in javascript. Foremost and most readable
There is a <input type=checkbox name=option2 id=checkbox_3 value=3 /> I tried this code <script

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.