--- id: 56533eb9ac21ba0edf2244ca title: Using Objects for Lookups challengeType: 1 videoUrl: '' localeTitle: 使用对象进行查找 --- ## Description
对象可以被认为是键/值存储,就像字典一样。如果您有表格数据,则可以使用对象“查找”值而不是switch语句或if/else链。当您知道输入数据限制在特定范围内时,这非常有用。以下是简单反向字母查找的示例:
var alpha = {
1: “Z”,
2: “Y”,
3: “X”,
4: “W”,
...
24: “C”,
25: “B”,
26: “A”
};
阿尔法[2]; //“Y”
阿尔法[24]; // “C”

var value = 2;
阿尔法[值]。 //“Y”
## Instructions
将switch语句转换为名为lookup的对象。使用它来查找val并将关联的字符串分配给result变量。
## Tests
```yml tests: - text: phoneticLookup("alpha")应该等于"Adams" testString: 'assert(phoneticLookup("alpha") === "Adams", "phoneticLookup("alpha") should equal "Adams"");' - text: phoneticLookup("bravo")应该等于"Boston" testString: 'assert(phoneticLookup("bravo") === "Boston", "phoneticLookup("bravo") should equal "Boston"");' - text: phoneticLookup("charlie")应该等于"Chicago" testString: 'assert(phoneticLookup("charlie") === "Chicago", "phoneticLookup("charlie") should equal "Chicago"");' - text: phoneticLookup("delta")应该等于"Denver" testString: 'assert(phoneticLookup("delta") === "Denver", "phoneticLookup("delta") should equal "Denver"");' - text: phoneticLookup("echo")应该等于"Easy" testString: 'assert(phoneticLookup("echo") === "Easy", "phoneticLookup("echo") should equal "Easy"");' - text: phoneticLookup("foxtrot")应该等于"Frank" testString: 'assert(phoneticLookup("foxtrot") === "Frank", "phoneticLookup("foxtrot") should equal "Frank"");' - text: phoneticLookup("")应该等于undefined testString: 'assert(typeof phoneticLookup("") === "undefined", "phoneticLookup("") should equal undefined");' - text: 您不应该修改return语句 testString: 'assert(code.match(/return\sresult;/), "You should not modify the return statement");' - text: 您不应该使用caseswitchif语句 testString: 'assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,"")), "You should not use case, switch, or if statements"); ' ```
## Challenge Seed
```js // Setup function phoneticLookup(val) { var result = ""; // Only change code below this line 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"; } // Only change code above this line return result; } // Change this value to test phoneticLookup("charlie"); ```
## Solution
```js // solution required ```