views:

46

answers:

3

I would like to dynamically create and reference some variables on the fly, but i'm not understanding how to.

Here is what I would think 'should' work, but I know doesn't.

var weeks = 4;
 for(i=0; i<weeks.length;i++){
  var 'week_'+i = valueFromXML;
}



function wtFn (){
  'week_'+i.splice(-1, 1);
  if('week_'+i.length <=0){
    $(this).parent().parent().slideUp();
  }
}

I'm open to suggestions. Thanks in advance.

+2  A: 

You can't declare variables dynamically it without using eval, and that is not considered as a good practice.

I would recommend you to use an object to store your values as properties:

var weeks = 4;
var obj = {};
for(var i = 0; i< weeks; i++){
  obj['week_'+i] = valueFromXML;
}

Then you can access the properties like:

alert(obj['week_'+i]);
CMS
+1  A: 

You want to use arrays.

var weeks = new Array();

for(i=0; i < weeks.length;i++) {
  weeks[i] = valueFromXML;
}
Spencer
I think what I'm looking to do is actually create an object on the fly, just like the above answer, but also dynamically create a series of arrays inside the object.for(i=0; i < weeks.length;i++) { obj['week'+i] = []; obj['week'+i].push(day);}
Jason
@Jason: that should work just fine...
Shog9
@Shog9 - it does seem to work, but i can't advance the array position to fill the next spot... tempVar['week'+i].push(day) needs to be the same as tempVar.week0[i].push(day), for example. but I can't figure out how to proper add the [i] for the array position.
Jason
@Jason: Are you trying to implement a two-dimensional sparse array? If so, there are probably easier ways...
Shog9
@Shog9 - i think the two-dimensional array is what i want to do... dynamically create an object with multiple elements inside. Each inside element is an array that will be updated/removed periodically. But like I said, im just not sure how to advance the position of the array.
Jason
@Jason: I think you should probably ask a new question... Because I'm starting to think you've latched on to a solution that doesn't really fit your problem. This time around, describe what your goals are, how you intend to *use* this structure and the data therein. And then ask, "how should I store this?" - chances are, it's a whole lot simpler than you think...
Shog9
I agree with @Shog9. You seem to have wavered from your initial problem quite a bit. And there are lots of different ways you can store and access data in js/jquery.
Spencer
Jason
A: 

As noted by others, arrays or objects are the way to go. But if you really must, you can sort of create dynamic variables -- or rather you create members of the global object, window. Assuming this is executing in a browser, do this:

var weeks = 4;
 for(i=0; i<weeks.length;i++){
  var window['week_'+i] = valueFromXML;
}



function wtFn (){

  window['week_'+i].splice(-1, 1);
  if(window['week_'+i].length <=0){
    $(this).parent().parent().slideUp();
  }
}

This works because in a browser, window['xyz'] will return the same object as xyz (that's assuming xyz wasn't created inside a function using the keyword var).

Andrew