${"dynamic" + $i} variable names in javascript

Hi, wondering if anyone could help with this one. In php you can do something like this:

$rownum = 3;

$val[1] = ‘val1’;
$val[2] = ‘val2’;
$val[3] = ‘val3’;

for($i=1; $i < rownum; $i++)
{
${“dynamic”.$i} = $val[$i];
}

This would result in these variables being created etc:

$dynamic1 = ‘val1’
$dynamic2 = ‘val2’
$dynamic3 = ‘val3’

However I can’t work out how to create a dynamic variable name in javascript.

var {“dynamic” + i}
var (“dynamic” + i)
var [“dynamic” + i]
var “dynamic” + i

None of these seem to work. Any ideas?

Thanks

Damian

Hello

By dynamic variables I think you’re referring to arrays? There’s a few ways to accomplish what you ask. Here’s an example:



var myArray = {
   value1: "hello",
   value2: "what's up",
   value3: "not much"
}

for (var i in myArray) {

   alert(test[i]);

}



You can create dynamic variables with eval.

eval(“dynamic” + i + " = val[i]");

Why not just use an array? It’s easier to work with and read.

I agree that arrays and objects are simpler. I believe there could be reasons for using such trick.

Thanks guys, I knew someone would know the answer.

eval() looks like it could be what i’m looking for. I will try this when I’m back at work tomorrow.

I basically need to pass these variables into an ajax GET request so I can use them in the requested php script. I can’t do that with arrays.

Although it just dawned on me that I can use POST as well as the GET method when using the XMLHttpRequest object!

Anyhow it’s useful to know because I often use this technique for creating variables when I need to (or can’t think of a better way to do it).

Cheers

You definately don’t need eval() or variable variables here. All you need to do is dynamically create strings, which doesn’t require eval();

php even supports a convenient way to receive variable names with array like notation via get post cookie, which it will parse into arrays for you. It even supports both numerical and string indexed arrays.
http://www.php.net/manual/en/faq.html.php#faq.html.arrays

With and without that php feature, you’re still just making strings.


urlParams = [];
for (i=0; i<myList.length; i++) {
    queryStringName = 'foo' + i;
    urlParams.push(queryStringName + '=' + myList[i]);
}
alert(urlParams.join('&'));

// or php specific array feature
urlParams = [];
for (i=0; i<myList.length; i++) {
    urlParams.push('foo[]=' + myList[i]);
}
alert(urlParams.join('&'));


In javascript and php at least, it’s extremely rare that variable variables are a better solution than arrays.

Or you could use an object.

var o = new Object;
o.property = "value"; // is functionally equivalent to
o["property"] = "value";

Cheers,
D.

Cheers guys, thanks for your help.

Hi

I’m new here, but will probably be racking up the post counts with my javascripting ignorance.

I’m having what seems to be a similar problem to this initial one and neither of the solutions (eval or arrays) seems to work for me.

In this code, using the eval example, I need to load dynamic variable values (some pseudo code in loop)

Any help greatly appreciated!

var myPanels=new Array("Email","News","Finance");
	
for (i=0;i&lt;myPanels.length;i++) {

	eval("div" + i + " = myPanels[i]");
	//.. so that I end up with 'divEmail', 'divNews', 'divFinance'
	
	
	eval("dynamic" + i) = new customobject.that.I'm.instantiating("blah","blah");
	eval("dynamic" + i).render();
	//..and in these two lines I'm creating a new object with each of the new variables, and doing something with them
	
			
}

eval(“div” + i + " = myPanels[i]");
//… so that I end up with ‘divEmail’, ‘divNews’, ‘divFinance’

that will create this: div1 = ‘Email’, div2=‘News’

Y do you want to use that? Y not just loop thru the panels array and do all u need to do in there, instead of creating dynamic variables?

What is it gona be used for?

var myPanels=new Array("Email","News","Finance");

// object, often used like associative array
var div = {};

// array
var dynamic = [];

for (i=0;i<myPanels.length;i++) {
    div[myPanels[i]] = 'I am div.' + myPanels[i];
    dynamic[i] = new customobject.that.Im.instantiating("blah","blah");
    dynamic[i].render();
}
alert(div.Email)
alert(div['News'])
var key = 'Finance';
alert(div[key])

I challenge someone to post some realistic code that is better served(readability, speed, organization) by using eval() to create variable variables opposed to arrays or objects. There’s very few meaningful cases.

Is better coded as:

window["div"+i] = myPanels[i];

which produces exactly the same resulting fields which can be used exactly the same way but without the inefficiency of the eval call.

window[‘divEmail’] is the same as window.divEmail is the same as divEmail - just three different ways of referencing the one variable.

crmalibu ,thanks very much for your solution -it works perfectly.

Cheers!
Mark