Selecting values from 2 javascript variables
I have got 2 javascript variables:
var list1 = "john do开发者_高级运维e|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?!
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"
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];
}
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/
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.
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"]);
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.
精彩评论