Storing tree data in Javascript

Posted by Ozh on Stack Overflow See other posts from Stack Overflow or by Ozh
Published on 2012-11-07T15:41:33Z Indexed on 2012/11/09 23:00 UTC
Read the original article Hit count: 151

Filed under:

I need to store data to represent this:

Water + Fire = Steam
Water + Earth = Mud
Mud + Fire = Rock

The goal is the following: I have draggable HTML divs, and when <div id="Fire"> and <div id="Mud"> overlap, I add <div id="Rock"> to the screen. Ever played Alchemy on iPhone or Android? Same stuff

Right now, the way I'm doing this is a JS object :

var stuff = {
    'Steam' : { needs: [ 'Water', 'Fire'] },
    'Mud'   : { needs: [ 'Water', 'Earth'] },
    'Rock'  : { needs: [ 'Mud',   'Fire'] },
    // etc...
};

and every time a div overlaps with another one, I traverse the object keys and check the 'needs' array.

I can deal with that structure but I was wondering if I could do any better?

Edit: I should add that I also need to store a few other things, like a short description or an icon name. So typicall I have Steam: { needs: [ array ], desc: "short desc", icon:"steam.png"},

© Stack Overflow or respective owner

Related posts about JavaScript