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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:29:06+00:00 2026-06-17T21:29:06+00:00

Is there a simpler (or more efficient) way of achieving the following: var _dataObjects

  • 0

Is there a simpler (or more efficient) way of achieving the following:

var _dataObjects = [{id:0, data:"data0", nextID:1}, 
                    {id:1, data:"data1", nextID:2}, 
                    {id:2, data:"data2", nextID:3} .. etc.];
generateNextPieceOfData();

function generateNextPieceOfData(){
    var len = _dataObjects.length;
    for ( var i = 0; i < len; i ++ ) {
        var nextDataID = _dataObjects[i].nextID;
        var nextData;
        for ( var j = 0; j < len; j ++ ) {
            if( _dataObjects[j].id == nextDataID ){
                nextData = _dataObjects[j].data;
                break;
            }
        }
    }
}

The above example is abstracted from the problem I’m having and I realise the ID numbers are sequential in this instance but in the real problem nextID numbers do not run sequentially.

Thanks 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-06-17T21:29:07+00:00Added an answer on June 17, 2026 at 9:29 pm

    Use the right data structure for your problem. Since you want to find an object by ID, create a hash map with the IDs as keys and objects as values:

    var object_map = {};
    
    for(var i = 0, l = _dataObjects.length; i < l; i++) {
        objects[_dataObjects[i].id] = _dataObjects[i];
    }
    

    Then getting the next object is simply:

    var next_object = object_map[someObject.nextID];
    

    You still have iterate until some terminal condition is met though. For example:

    function generatePath(id_a, id_b) {
        var obj = object_map[id_a];
        var path = [obj];
    
        while (obj && obj.id !== id_b) {
            obj = object_map[obj.nextID];
            path.push(obj);
        }
        return path;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there was a more efficient (efficient as in simpler/cleaner code)
Is there a simpler way to rewrite the following condition in JavaScript? if ((x
is there a more simple way to get combobox (WinForms) selected items text? string
Is there a simpler way of implement this? Or a implemented method in JDK
Is there a simpler way to get a string of a parent directory than
Given two lists of equal length, is there a simpler or preferred way to
Is there a simple and efficient way to get the number of visitors who
I'm looking for a more efficient way to reprioritize items in a priority queue.
I'm trying to work out a more efficient way to add a note count,
So yeah, I'm just trying to see if there is a more elegant way

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.