Automatically create new objects? (with programmatic names)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roadworrier
    New Member
    • May 2007
    • 2

    Automatically create new objects? (with programmatic names)

    I'm trying to create a new object to use as an associative array automatically.

    I can programmaticall y create new variables like this:
    Code:
    for (var i; i < 10; i++) {
      window["foo" + i];
      window["foo" + i] = "moo" + i;
    }
    // now I have foo0,foo1, foo2, foo3... foo9
    And I can create a new object like this:
    Code:
    var myObject = new Object();
    but I don't know how to create for example
    Code:
    for (...) {
      var myObject + i = new Object();
    }
    I want to do this because I'm going to make a cgi call to a C program which includes a date range parameter and various other things. Once I get a large array back I'm going to calculate median, standard deviation, spikes, etc. I want to save the array and those calculations in an object, since I'll potentially get 40 different results back. If I save these properties, then I can cache the calculations and cgi query results, so I don't have to make the same request/calculations twice.

    So, I'd like the object name to be something close to my cgi query string. Then before I make the query again, I can see if I already have the object. If I do, I have the data, and the calculations.

    When a new date range is selected, though, I would like to be able to delete all the cached objects so they aren't eating browser memory.

    Any pointers?

    I can do this with a whole slew of automatically created variables, but it would be nicer and more logical to do via objects.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Originally posted by roadworrier
    I want to do this because I'm going to make a cgi call to a C program which includes a date range parameter and various other things. Once I get a large array back I'm going to calculate median, standard deviation, spikes, etc. I want to save the array and those calculations in an object, since I'll potentially get 40 different results back. If I save these properties, then I can cache the calculations and cgi query results, so I don't have to make the same request/calculations twice.
    You actually had it pretty much; just combine what you were doing:

    [CODE=JavaScript]
    for(var $i = 0; $i < 10; $i++)
    window['myObject' + $i] = {}; // or = new Object(); your choice...

    // E.g.:
    for(var $r = 0; $r < 10; $r++)
    for(var $c = 0; $c < 10; $c++)
    window['myObject' + $r][$c] = ($r * $c);

    // Etc....
    [/code]

    Comment

    • roadworrier
      New Member
      • May 2007
      • 2

      #3
      Originally posted by pbmods
      window['myObject' + $i] = {};
      Perfect! Thanks. It appears to do just what I want in my tests. Somehow I didn't think I could create objects as properties of the object 'window' the way I could with regular variables/properties.

      Much appreciated.

      Comment

      Working...