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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:58:07+00:00 2026-05-27T08:58:07+00:00

Overview So I have pulled out a document from my database. Inside is a

  • 0

Overview

So I have pulled out a document from my database. Inside is a nested collection of objects. Each of the objects inside this nested collection has an ‘_id’ attribute. I want to find one on these objects specifically by its ‘_id’ using Javascript.

Example

http://jsfiddle.net/WilsonPage/tNngT/

Alternative Example

http://jsfiddle.net/WilsonPage/tNngT/3/

Questions

  1. Is my example the best way of achieving this?
  2. Will this block in Node.js?
  • 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-27T08:58:08+00:00Added an answer on May 27, 2026 at 8:58 am

    Yes, if you only know a specific value which is contained by one of your objects (which are held in an Array) you need to loop over the whole structure and compare those values.

    As you also did right, break the iteration when you found one (return in your example).
    So my answer to your first question would be yes, in terms of performance this is the right and best way.

    What I don’t get is the “Async” example. You just moved the code and changed the structure. Your code is still “blocking” since you’re using a normal for-loop to search. If that array would be huge, it would block your node-app for the amount of time the loop needs to finish.

    To really make it asynchronously, you would need to get rid of any loop. You would need to loop over the structure with a runway-timer.

    var findById = function(collection, _id, cb){
        var coll = collection.slice( 0 ); // create a clone
    
        (function _loop( data ) {
            if( data._id === _id ) {
                cb.apply( null, [ data ] );
            }
            else if( coll.length ) {
                setTimeout( _loop.bind( null, coll.shift() ), 25 );
            }
        }( coll.shift() ));
    };
    

    And then use it like

    findById( myCollection, 102, function( data ) {
        console.log('MATCH -> ', data);
    });
    

    This technique (which is a simplified example), we are creating an self-invoking anonymous function and we pass in the first array item (using .shift()). We do our comparison and if we found the item we are looking for, execute a callback function the caller needs to provide. If we don’t have a match but the array still contains elements (check for the .length), we create a timeout of 25ms using setTimeout and call our _loop function again, this time with the next array item, because .shift() gets and removes the first entry. We repeat that until either no items are left or we found the element. Since setTimeout gives other tasks in the JS main thread (on a browser, the UI thread) the chance to do things, we don’t block and screw up the whole show.

    As I said, this can get optimized. For instance, we can use a do-while loop within the _loop() method and also use Date.now() to do things until we go over the 50ms mark for instance. If we need longer than that, create a timeout the same way and repeat the above described operation again (so its like, do as much operation as possible within 50ms).

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

Sidebar

Related Questions

I have to make this overview of questions and the user has to be
I have an overview list, with divs, and also each div has a pair,
I have a overview page containing a list with some links from which multiple
I have a document that is an overview document within a folder, there are
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int
Overview: I have two text boxes at the top of this form for inputting
apologies if this a bit vague but I don't have the overview to be
I have 2 UIScrollViews displaying an image each (overview and zoom mode). I can
Overview: I have a Silverlight 4 app where I am seeing problematic behavior from
I'd like some kind of overview tutorial that shows how this works. 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.