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.

4
  • 4
    What does the code do? What is its purpose? Is it supposed to return anything, because it does not seem to do anything useful. Commented Jan 26, 2013 at 10:57
  • The code will eventually decide a path between nodes in 3D space based on the id and nextID values. As I mentioned in my question, the example code above is abstract but is representative of my problem so it won't do anything useful. If I were to post the problem with full context it would be superfluous. Commented Jan 26, 2013 at 11:03
  • Yeah, but it would still more useful to let the function have a proper input and output. Can I assume that you pass it two IDs and finding the path means to find all objects from the first ID to the second ID (following nextID)? In any case, if you use an object has hash map, where the IDs are the keys and the objects are the values, you can easily find the next object. Commented Jan 26, 2013 at 11:06
  • find last next id, last nextdata all the time and not necessarily related ones. and even those r local variables.. Commented Jan 26, 2013 at 11:10

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

If your code works sequentially only, then you can sort the items by id or whatever and your code should work right? Try this:

_dataObjects = _dataObjects.sort(function(a, b) {
  return a.id > b.id;
});

3 Comments

I appreciate you taking your time to answer but: "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."
??? This already assumes that your _dataObjects array is not ordered sequentially. Have you tried the code?
I did, but I didn't explain my problem very I apologise and this just wasn't what I needed. Thanks for helping though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.