JavaScript 使用对象进行查找值

对象和字典一样,可以用来存储键/值对。如果你的数据跟对象一样,你可以用对象来查找你想要的值,而不是使用switch或if/else语句。当你知道你的输入数据在某个范围时,这种查找方式极为有效。

这是简单的反向字母表:

var alpha = {
1:"Z",
2:"Y",
3:"X",
4:"W",
...
24:"C",
25:"B",
26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

 例:

switch语句查找:

function phoneticLookup(val) {
var result = "";
switch(val) {
case "alpha":
result = "Adams";
break;
case "bravo":
result = "Boston";
break;
case "charlie":
result = "Chicago";
break;
case "delta":
result = "Denver";
break;
case "echo":
result = "Easy";
break;
case "foxtrot":
result = "Frank";
}
return result;
}
phoneticLookup("charlie");

 

 

对象查找:

function phoneticLookup(val) {
var result = "";
var lookup={
    "alpha":"Adams",
    "bravo":"Boston",
    "charlie":"Chicago",
    "delta":"Denver",
    "echo":"Easy",
    "foxtrot":"Frank"
}
result = lookup[val];
return result;

phoneticLookup("charlie");

 输出结果:Chicago

你可能感兴趣的:(javascript)