Home
TeamSite
Add dynamic properties to JS Object
cliffhanger
Is it possible to add properties(params) to a JavaScript object dynamically??
Something like:
var parameters = new Object();
for(var i=1; i<=N; i++)
{
parameters.("myParam_"+i) = SOME_VALUE;
}
??
I've already tried this, and it gives me an error that I was expecting : "identifier expected". Wondering if there's another way of achieving this functionality, and/or some way of manipulating the parameter string so that JS considers it as a identifier.
Any help is appreciated
Thanks!!
-cliff
Find more posts tagged with
Comments
Adam Stoller
Just a shot in the dark - i.e. untested - perhaps:
var parameters = new Object();
for(var i=1; i<=N; i++){
var key = "myParam_"+i;
parameters.(
key
) = SOME_VALUE;
}
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
JonathonG
You have to use 'eval' IIRC, it looks something like:
eval("parameters.myParam_ " + i +") = SOME_VALUE");
I may not have that exactly right, but hopefully it will put you on the right track.
Jonathon
Interwoven Developer
Allstate, Inc.