...

View Full Version : how to map one array element to another array element?



shy84may
05-08-2006, 05:52 AM
hi..
since i'm still new in programming..i would like to ask anyone's opinion on my problem with javascript array.

if let say i have 2 arrays :

var name = ["Mary","Adam","Aziz"]
var id = ["84572","77617","96432"]

how cam i map Mary to 84572 and so on..?

please..i'm really stuck in here..

thanks :confused:

Kravvitz
05-08-2006, 08:14 AM
Well, "Mary" and "84572" have the same index in the array, which is 0.

If that doesn't answer your question, then you will need to give us more information.

Kor
05-08-2006, 10:43 AM
Mybe using new objects:


<script type="text/javascript">
var name = ["Mary","Adam","Aziz"]
var id = ["84572","77617","96432"]
for(var i=0;i<name.length;i++){
window[name[i]]={'id':id[i]}
}
alert('Mary\'s id= '+Mary.id+' Adam\'s id= '+Adam.id+' Aziz\'s id= '+Aziz.id)
</script>

But, as Kravvitz said, give us more details about your aim.

shy84may
05-09-2006, 03:51 AM
what i wanted to do is to produce a code where user should enter "unit" in strings (eg IT Dept) and the system should be able to interpret the "unit" into a single character (eg IT Dept == 'A') whenever the user click on "VERIFY CODE" button and the character 'A' (in this case) should appear in one textbox named "unitcode" which is concatenated with constant 'J0020'.
i've tried using the if..else function
eg.
function verify_code()
{
if (unit = "IT Dept)
unitcode == 'A';
else (unit = "Engineering Dept)
unitcode == 'B';
else.....so on...
}
document.reka_kursus.unitcode.value=('J0020' + unitcode);

but the problem is still when user clicks "VERIFY CODE" button, the system will return the whole list of code in character
(eg: J0020A,B,C,D,E,F.....bla.bla..)

please..this is not the output i want.
Thanks to everyone who is willing to help me.. :rolleyes:

Kravvitz
05-09-2006, 04:11 AM
Without seeing your actual code it's difficult to help.

In the following code snippet you have "=" and "==" backwards.

if (unit = "IT Dept)
unitcode == 'A';
else (unit = "Engineering Dept)
unitcode == 'B';

shy84may
05-09-2006, 04:25 AM
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
var arrItems1 = ["PPLB","Management","Administration","Finance","Corporate System","Computer","Electrical","Electronic & Instrument","Mechanical","Safety","Welding","Non Destructive Testing","Operation","Others","Overhead","Cable","Substation","Others","Overhead","Cable","Substation","Others"]
var arrItemsGrp1 = ["11","21","21","21","21","31","41","41","41","41","41","41","41","41","51","51","51","51","61","61","61","61"]
var arrunitcode = ["R","A","B","C","D","S","E","F","G","H","I","J","K","V","O","P","Q","T","L","M","N","U"]

function verify_code(arrItems1,arrItemsGrp1,arrunitcode) //to be executed when user click "VERIFY CODE" button
{
for(var i=0;i<arrItems1.length;i++)
{
window[arrItems1[i]]={'arrunitcode':arrunitcode[i]}
}
document.reka_kursus.kod_kursusb.value=('J0020'+arrItems1.arrunitcode);
}
// -->
</script>
</head>

<body>
<form name="reka_kursus" method="POST">

<p align="left"><b><font color="#003399" face="Arial">&nbsp; Kod Kursus :</font>
&nbsp;<input type="text" name="kod_kursusb" size="11" maxlength="11">&nbsp;</p>
<p align=center><input type="button" value="VERIFY CODE" onclick="verify_code();" name="VERIFY CODE">


/* for now, this is my real coding */

shy84may
05-09-2006, 04:45 AM
for now..the output doesn't appear when user click that button..

since i am so new in programming, i can't debug my own program..

thanks to kravvitz for ur help..:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum