Forum Moderators: open

Message Too Old, No Replies

Arranging data in JavaScript

         

ocon

11:46 pm on Mar 22, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a table of celestial bodies and their data. I need to arrange these information in an accessible way that would allow me to easily add more celestial bodies (or remove existing ones), loop through all bodies, and be able to grab data points for an arbitrary body on demand using it's name and not it's position (without having to loop through everything). Even though it's a bit clunky, I was able to meet my first to objectives now.

var celBody = [
{'id':'Sun', 'data':[696342, 696342, 0, 0, 0]},
{'id':'Mercury', 'data':[2439.7, 2439.7, 0.449505, 0.35758, 223.74798]},
{'id':'Venus', 'data':[6051.8, 6051.8, 0.7188755, 3.34309, 155.5424]},
{'id':'Earth', 'data':[6378.137, 6356.7523, 0.9850864, 0, 128.14849]},
{'id':'Mars', 'data':[3396.2, 3376.2, 1.4345873, -0.61867, 28.94692]}];

I'd like to arrange these elements in a manner more like PHP that would allow me to do something like $celBody["Earth"][3].

Is this possible?

Fotiman

1:30 am on Mar 23, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you change your structure slightly, you can have the equivalent of a map. For example:

var celBody = {
'Sun': [696342, 696342, 0, 0, 0],
'Mercury': [2439.7, 2439.7, 0.449505, 0.35758, 223.74798],
'Venus': [6051.8, 6051.8, 0.7188755, 3.34309, 155.5424],
'Earth': [6378.137, 6356.7523, 0.9850864, 0, 128.14849],
'Mars': [3396.2, 3376.2, 1.4345873, -0.61867, 28.94692]
};

Then you can do:
celBody["Earth"][3] (which in this case would give the value 0).

ocon

9:39 pm on Mar 23, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you very much, that's awesome! As I'm adjusting my code there are a couple things that now seem clunky:

  • To easily loop through the bodies, is this the best way:

    for(i = 0; i < Object.keys(celBody).length; i++){}

  • Is the easiest way to get a key name by index:

    Object.keys(celBody)[i]

  • Is this the best way to get a data field by index of the body:

    celBody[Object.keys(celBody)[i]][3]
  • Fotiman

    1:05 am on Mar 24, 2015 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




    To easily loop through the bodies, is this the best way:

    To loop through the bodies, it's probably better to do this:

    var i, n;
    for (i = 0, n = Object.keys(celBody).length; i < n; i++) { }

    This way, you're not evaluating Object.keys each time through the loop. Note, though, that Object.keys is not supported by IE8 or lower, so if you wanted to support those old browsers, you'd need to add a polyfill.

    Alternatively, you could do something like this:

    for (var property in celBody) {
    if (celBody.hasOwnProperty(property)) {
    // do something with property and/or celBody[property]
    }
    }



    Is the easiest way to get a key name by index:

    That certainly seems like a way to do it, though if you're going to be calling it in a loop, store the result of Object.keys(celBody) in a variable first, so you don't need to keep calling Object.keys over and over.

    Is this the best way to get a data field by index of the body:

    Same comment as above. Make sure you put the result of Object.keys(celBody) in a variable first:

    var celBodyKeys = Object.keys(celBody);
    // looping stuff
    celBody[celBodyKeys[i]][3]

    But if you're not going to be accessing the properties by name (as in celBody['Sun']), then perhaps storing them in a map is not the best data structure. It seems like you're asking for the exact opposite of what you wanted in your first question.