469,916 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Associative Array?

Hi All:
I want to write a script to do name mapping. The argument "internal" is
the name to be mapped and the return value is the mapped name i.e. if
"apple" is the input "Xapple" is the output. However the argument may
not have an entry in the associative array. for e.g. if the argument is
"strawberry" the output should be "strawberry" as there is no mapping
for "strawberry". I wrote the following script but it does not work.
function GetExternal(internal)
{
var my_cars= new Array()
my_cars["apple"]="Xapple";
my_cars["peach"]="Xpeach";
my_cars["orange"]="Xorange";
my_cars["banana"]="Xbanana";
my_cars["plum"]="Xplum";

if(my_cars[internal]!="")
return my_cars[internal];
else
return internal;
}
Can someone kindly point out what might be wrong?

TIA.

Jul 20 '05 #1
2 2060


Ravi wrote:
I want to write a script to do name mapping. The argument "internal" is
the name to be mapped and the return value is the mapped name i.e. if
"apple" is the input "Xapple" is the output. However the argument may
not have an entry in the associative array. for e.g. if the argument is
"strawberry" the output should be "strawberry" as there is no mapping
for "strawberry". I wrote the following script but it does not work.
function GetExternal(internal)
{
var my_cars= new Array()
I guess
var my_cars = new Object();
is more appropriate. my_cars["apple"]="Xapple";
my_cars["peach"]="Xpeach";
my_cars["orange"]="Xorange";
my_cars["banana"]="Xbanana";
my_cars["plum"]="Xplum";

if(my_cars[internal]!="")
return my_cars[internal];
else
return internal;
}


if (typeof my_cars[internal] != 'undefined')
return my_cars[internal];
else
return internal;

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

"Ravi" <rg**@cse.buffalo.edu> wrote in message
news:bp**********@prometheus.acsu.buffalo.edu...
Hi All:
I want to write a script to do name mapping. The argument "internal" is
the name to be mapped and the return value is the mapped name i.e. if
"apple" is the input "Xapple" is the output. However the argument may
not have an entry in the associative array. for e.g. if the argument is
"strawberry" the output should be "strawberry" as there is no mapping
for "strawberry". I wrote the following script but it does not work.
function GetExternal(internal)
{
var my_cars= new Array()
my_cars["apple"]="Xapple";
my_cars["peach"]="Xpeach";
my_cars["orange"]="Xorange";
my_cars["banana"]="Xbanana";
my_cars["plum"]="Xplum";

if(my_cars[internal]!="")
return my_cars[internal];
else
return internal;
}


In JavaScript, the structure you want is Object, not array. Get used to the
Literal Object Notation.

var getExternal = function (internal) {
return getExternal.data[internal] || internal;
}
getExternal.data = {
apple: "Xapple",
peach: "Xpeach",
orange: "Xorange",
banana: "Xbanana",
plum: "Xplum"};

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Abdullah Kauchali | last post: by
6 posts views Thread by mark4asp | last post: by
4 posts views Thread by Robert | last post: by
8 posts views Thread by Derek Basch | last post: by
5 posts views Thread by soup_or_power | last post: by
7 posts views Thread by Robert Mark Bram | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
11 posts views Thread by Bosconian | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.