Associative Array?

R

Ravi

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.
 
M

Martin Honnen

Ravi said:
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;
 
D

Douglas Crockford

Ravi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top