The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
How to use string as Array name?
Discuss How to use string as Array name? in the JavaScript Development forum on Dev Shed. How to use string as Array name? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 4th, 2006, 10:00 AM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Posts: 285
  
Time spent in forums: 1 Day 19 h 59 m 41 sec
Reputation Power: 11
|
|
|
How to use string as Array name?
a = new Array('a1','a2','a3');
function myfun1(haha){
tmp2 = haha;
for(i=0;i<tmp2.length;i++){
document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]);
}
}
...............
myfun1('a');
-------------------------------------------
When I alert(tmp2), a is displayed. But I found that tmp2[i] won't evaluate as a[i]. Because it just treat tmp2 as an array.
-------------------------------------------
Can anyone help? Thx.
|

April 4th, 2006, 01:12 PM
|
 |
Application is what Divides Us
|
|
Join Date: Dec 2002
Location: Titusville, FL
|
|
At first glance I would say that the problem is that you are
passing the array name as a string, instead of the object itself.
Code:
myfun1('a') --> myfun1(a)
But then it comes down to, you want to explicity access
the array via it's string name? Could you explain why you'd
want to do it this way?
__________________
Download [ Fx | Op ] Validate [ Markup | Css ]
Last edited by jsKid : April 4th, 2006 at 01:14 PM.
|

April 4th, 2006, 03:22 PM
|
 |
garish grotesque gargoyle
|
|
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
|
|
|
you can get tricky...
I agree with jsKid, there doesn't seem to be any need to reference the array by a string containing its name. if you wanted to do this, it would be better to create a container with a string reference for the array, then use the string reference elsewhere... there's a couple ways to do this:
Code:
<html>
<head>
<title>JS wierdness</title>
<script type="text/javascript">
var arrCont = new Array();
arrCont['foo'] = new Array();
arrCont['foo'][0] = 'arrrr foooooooohh onnnneee';
arrCont['foo'][1] = 'arrrr foooooooohh twwwoooo';
arrCont['bar'] = new Array();
arrCont['bar'][0] = 'arrrr baaaaahhhrr onnnneee';
arrCont['bar'][1] = 'arrrr baaaaahhhrr twwwoooo';
var objCont = new Object();
objCont['foo'] = new Array();
objCont['foo'][0] = 'obbbby foooooooohh onnnneee';
objCont['foo'][1] = 'obbbby foooooooohh twwwoooo';
objCont['bar'] = new Array();
objCont['bar'][0] = 'obbbby baaaaahhhrr onnnneee';
objCont['bar'][1] = 'obbbby baaaaahhhrr twwwoooo';
warr = 'foo';
</script>
</head>
<body>
given two arrays, 'foo' and 'bar', access them via string names by reference in either an array or object container:
<a id='linker' href="javascript:warr=='foo'?warr='bar':warr='foo';void(document.getElementById('linkor').firstChild.data= 'currently '+warr+', click to change');"><span id="linkor">currently foo, click to change</span></a><br><br>
<a href="javascript:alert(arrCont[warr][0])">alert from array container, part one</a><br>
<a href="javascript:alert(arrCont[warr][1])">alert from array container, part two</a><br><br>
<a href="javascript:alert(objCont[warr][0])">alert from object container, part one</a><br>
<a href="javascript:alert(objCont[warr][1])">alert from object container, part two</a><br>
</body>
</html>
but in both cases you're really using a string key in a container object (I think this works because arrays are objects and that object notation is allowed in JS). If you *must* access arrays by string reference, just build them as members of a collection (even if there's only one member) so that you can use this notation to find them.
Is this what you needed?
HTH - derelict
|

April 5th, 2006, 12:19 PM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Posts: 285
  
Time spent in forums: 1 Day 19 h 59 m 41 sec
Reputation Power: 11
|
|
|
Yes, thank you, this is what I want. Though I am not fully know all the syntax (90% acheived), I think i can finish the rest of my code.
Actually, I would like to create 3 arrays, and by passing one argument to a function, it will extract the suitable array and return its values.
------------------
a = new Array('a1','a2','a3');
b = new Array('b1','b2','b3');
c = new Array('c1','c2','c3');
function myfun1(haha){
tmp2 = haha;
for(i=0;i<tmp2.length;i++){
document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]);
}
}
myfun1('b');
|

April 5th, 2006, 12:54 PM
|
 |
garish grotesque gargoyle
|
|
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
|
|
|
then this should work:
Javascript Code:
Original
- Javascript Code |
|
|
|
var arrCont = new Object; // create an object to contain your arrays by reference arrCont['a'] = new Array('a1','a2','a3'); arrCont['b'] = new Array('b1','b2','b3'); arrCont['c'] = new Array('c1','c2','c3'); // notice use of quotes around identifiers 'a','b', and 'c' function myfun1(haha){ // input string 'b' comes in as var haha tmp2 = arrCont[haha]; // notice *NO* quotes to deref input string from var context // and that it's not a direct assign... we're grabbing from the array container arrCont for(i=0;i<tmp2.length;i++){ document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]); } } myfun1('b'); // notice quotes becuase we're passing the string identifier, not a variable
this should make your script work the desired way. If you have more questions about syntax or principles, please just ask
HTH - derelict
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|