473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2145


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.