Click to See Complete Forum and Search --> : How do I get a variable's name instead of its value?


omelette
11-13-2003, 10:10 PM
I want the output:
"The names of my hippos are Hippogirl,Hippee".

My script:
lions = new Array("Lyan", "Grrr");
hippos = new Array("Hippogirl", "Hippee");
zoo_list = new Array(lions, hippos);

for (i=0; i<zoo_list.length; i++) {
document.write("My " + zoo_list[i] + " are " + zoo_list[i]);
}

Would anyone know what I should do to zoo_list[i] to get it to write "hippo" instead of "Hippogirl, Hippee"?

Jonathan
11-13-2003, 11:59 PM
not sure if this is what you wanted.. but here...


<script language="JavaScript" type="text/javascript">
<!--
lions = new Array("Lyan", "Grrr");
hippos = new Array("Hippogirl", "Hippee");
zoo_list = new Array(lions, hippos);
your_list = new Array("lion","hippo");

for (i=0; i<zoo_list.length; i++) {
for (j=0;j<your_list.length; j++){
document.write("My " + your_list[j] + " are " + zoo_list[i] + ". ");
}}

//-->
</script>

omelette
11-17-2003, 08:21 PM
Thanks, Jonathan! :) I'll try that. I guess then there is no way to extract the variable's name instead of making another list, right? Well, I guess it doesn't matter for my case coz I have a small list but for those who have a long list, er... oops, bad luck, eh? :eek:

Jonathan
11-18-2003, 05:03 PM
Sure anytime... Sorry I couldn't get what you asked for... But if you need anything... Just PM me..

AdamBrill
11-18-2003, 05:14 PM
You might be interested in this:<script type="text/javascript">
zoo_list = new Array();
zoo_list["hippos"] = new Array("Hippogirl", "Hippee");
zoo_list["lions"] = new Array("Lyan", "Grrr");
for(value in zoo_list){
document.write("My " + value + " are " + zoo_list[value] + "<br>");
}
</script>I hope that helps you! :)

omelette
11-19-2003, 08:31 PM
Hi AdamBrill, Yeah!! I think that's IT! I didn't know how to use associative arrays that way (ie. "value in..."), so yeah :cool: , I've learnt something new! My real code is a bit more complex than that, I'll work on it and let u know my progress.

Thanks a big bunch! And to Jonathan too. :)

AdamBrill
11-19-2003, 09:03 PM
No problem. If you have any trouble getting it to work, let me know... ;)