0

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.

0

3 Answers 3

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use arrays.

var weeks = new Array();

for(i=0; i < weeks.length;i++) {
  weeks[i] = valueFromXML;
}

7 Comments

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); }
@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: Are you trying to implement a two-dimensional sparse array? If so, there are probably easier ways...
@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: 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...
|
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.