dfro
dfro

Reputation: 27

javascript reference to other variable

I'm trying to create an array of objects where one of the properties of each object is a list of a few other objects from the same array. Similar to a hierarchy, each object has parent objects and children objects. I'm wondering what is the most efficient way of doing this. The first thought I had was to simply create an array of the unique IDs. So:

var network = [
{
    "id": "ABCD",
    "description": "some info",
    "parents": [""], //no parents
    "childern": ["EFG"]
},
{
    "id": "EFG",
    "description": "more info",
    "parents": ["ABCD"],
    "children": ["XYZ"]
},
{
    "id": "LMNOP",
    "description": "something",
    "parents": ["ABCD"],
    "children": ["XYZ"]
},
{
    "id": "XYZ",
    "description": "something",
    "parents": ["EFG", "LMNOP"],
    "children": [] //no child nodes
}

My concern with this approach is that everytime I want to act on a 'parent' or 'child', I only have a string of the name. I would need to search the array to match the string to whatever object has that ID. So while I can get the description of my current object with network[2].description, I can't do something like network[2].children[0].description. If the array contains hundreds or even a couple thousand objects and I'm continually searching it I'm worried about performance.

The second approach I've considered is storing the index of the parent and children nodes instead of their unique IDs. Then I could access a parent or child node with something like network[network[1].children[0]]. But in this case I'm relying on the order of objects to remain static. I plan to pull down the data from a noSQL database at the start of each user session, so I don't think I can rely on the order to remain unchanged.

Is there another approach? As I think about C++ I would probably be able to store a pointer, but I don't think I can do that in JS.

Upvotes: 2

Views: 56

Answers (2)

Matt Morgan
Matt Morgan

Reputation: 5303

I suggest you store your objects in an object, not an array. You can then use the id as the key (aka pointer) to look up the object:

 var network = {
      "ABCD": {
          "id": "ABCD",
          "description": "some info",
          "parents": [""], //no parents
          "children": ["EFG"]
      },
      "EFG": {
          "id": "EFG",
          "description": "more info",
          "parents": ["ABCD"],
          "children": ["XYZ"]
      },
      "LMNOP": {
          "id": "LMNOP",
          "description": "something",
          "parents": ["ABCD"],
          "children": ["XYZ"]
      },
      "XYZ": {
          "id": "XYZ",
          "description": "something",
          "parents": ["EFG", "LMNOP"],
          "children": [] //no child nodes
      }
    }

    // access a parent like this
    let parent0 = network["XYZ"]["parents"][0];
    console.log(parent0);

    // get an array of your node IDs like this
    let nodeIds = Object.keys(network);
    console.log(nodeIds);

    // can still look up by position then
    console.log(network[nodeIds[0]]);

Upvotes: 1

Pablo Recalde
Pablo Recalde

Reputation: 3571

Of course you can do it using references to your objects

var a = {
    "id": "ABCD",
    "description": "some info",
    "parents": [], //no parents
    "children": []
};
var b = {
    "id": "EFG",
    "description": "more info",
    "parents": [],
    "children": []
};
var c = {
    "id": "LMNOP",
    "description": "something",
    "parents": [],
    "children": []
};
var d = {
    "id": "XYZ",
    "description": "something",
    "parents": [],
    "children": [] //no child nodes
};

a.children.push(b);
b.parents.push(a); 
//and so on.

var network = [a,b,c,d];

Upvotes: 0

Related Questions