views:

47

answers:

6

I have got 2 javascript variables:

var list1 = "john doe|bill williams|poll bregg|michael jordan";
var list2 = "analytic|trader|doctor|athlete";

We have to take in mind, that relations between this two variables are that names and professions are placed in the same order, eg. poll bregg is a doctor, but john doe is an analytic.

Than I need to create a function, which is going give me 2nd variable value based on 1st variable selected value. For example:

function getProfession(name){
    ...
    return profession;
}

Can you suggest a solution or give a clue?! Perhaps I have to use array of array or smth like this?!

A: 

Its better to use the array like this:

var persons = new Array();
persons[0] = new Array();
persons[0]["name"] = "John Doe";
persons[0]["proffesion"] = "Analytic";
persons[1] = new Array();
persons[1]["name"] = "Bill Williams";
persons[1]["proffesion"] = "Trader";
etc...

and than you can iterate it like a normal array:

for(i in persons)
   alert(persons[i]["name"]+" - "+persons[i]["proffesion"]);
tuffkid
It's probably better to use `persons[0] = {};` instead of `persons[0] = new Array();`, because you would not be using any of the array properties for these objects.
Daniel Vassallo
... And in general, `for ... in` loops are not considered good practice when iterating over array elements.
Daniel Vassallo
+2  A: 

You may find it easier to use the hashtable nature of JavaScript objects instead:

var professions = {};              // our hashtable

var list1 = "john doe|bill williams|poll bregg|michael jordan".split('|');
var list2 = "analytic|trader|doctor|athlete".split('|');

for (var i = 0; i < list1.length; i++) {
   professions[list1[i]] = list2[i];
}

Then you can get the professions by using the subscript syntax:

alert(professions['john doe']);    // returns "analytic"
Daniel Vassallo
The easiest way!
Nikita Sumeiko
+1  A: 
var persons = {};
var list1 = "john doe|bill williams|poll bregg|michael jordan".split("|");
var list2 = "analytic|trader|doctor|athlete".split("|");
for (var i in list1) {
    persons[list1[i]] = list2[i];
}

function getProfession(name) {
    return persons[name];
}
jsonx
Best answer, but duplicate. Anyway, thanks!
Nikita Sumeiko
+1  A: 

You can do in this way, using split and indexOf Javascript functions:

var list1 = "john doe|bill williams|poll bregg|michael jordan";
var list2 = "analytic|trader|doctor|athlete";

var names = list1.split("|");
var professions = list2.split("|");

function getProfession(name){
    var index = names.indexOf(name);

    return professions[index];
}

alert(getProfession('poll bregg'));

Full example in: http://jsfiddle.net/zQXjd/

Impiastro
Like when guys showing real examples in jsfiddle for example!
Nikita Sumeiko
A: 

Instead of 2 variabled you can use one object to get your functionality -

var profession = { "john doe" : "analytic",
           "bill williams" : "trader",
           "poll bregg" : "doctor",
           "michael jordan" : "athlete"
         };

Then in function you can use this as -

function getProfession(name) {  
    return profession[name];

}

From maintainence point of view also, this is easier for you to add new elements and remove existing elements.

Sachin Shanbhag
+1  A: 

the code could be like this.

var list1 = "john doe|bill williams|poll bregg|michael jordan";
var list2 = "analytic|trader|doctor|athlete";
var name_array=new Array();
var profession_array=new Array();
name_array=list1.split("|");    
profession_array=list2.split("|");    
function getProfession(name)    
{
    for (i=0;i<name_array.length;i++)
    {
        if(name==name_array[i])
        {
             return profession_array[i];
        }
    }
    return "Not in list";    
}

The code is possible if only the data is in the specified format.

srinivas
Looks too hard, but anyway works for me!
Nikita Sumeiko