Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable name using array element, and set value

Status
Not open for further replies.

peterpann

Programmer
Jun 19, 2007
63
GB
using IE8
var valuedaniel = 66;
var myarray = new Array();
myarray[0] = "daniel";
alert(eval("value" + myarray[0] ) ); //66
but
eval("value" + myarray[0] ) = 77; //cannot assign to a function result

how to set ("value" + myarray[0] ) to 77 so that valuedaniel will = 77 ?
Thanks.
 
Hi

Supposing your question is just for your curiosity and not intend to actually use it ever :
JavaScript:
[COLOR=orange]eval[/color][teal]([/teal][i][green]"value"[/green][/i] [teal]+[/teal] myarray[teal][[/teal][purple]0[/purple][teal]] +[/teal] [i][green]"= 77"[/green][/i][teal]);[/teal]

Feherke.
feherke.ga
 
Thanks Feherke. Is there a better way of doing it, to avoid using eval() ?
 
Hi

Make value an object and use the value.daniel property :
JavaScript:
[b]var[/b] value [teal]= {[/teal]daniel[teal]:[/teal] [purple]66[/purple][teal]};[/teal]
[b]var[/b] myarray [teal]= [];[/teal]
myarray[teal][[/teal][purple]0[/purple][teal]] =[/teal] [i][green]'daniel'[/green][/i][teal];[/teal]
[COLOR=orange]alert[/color][teal]([/teal]value[teal][[/teal]myarray[teal][[/teal][purple]0[/purple][teal]]]);[/teal]
value[teal][[/teal]myarray[teal][[/teal][purple]0[/purple][teal]]] =[/teal] [purple]77[/purple][teal];[/teal]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top