freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../basic-javascript/using-objects-for-lookups/index.md

1.5 KiB

title localeTitle
Using Objects for Lookups 使用对象进行查找

使用对象进行查找

这是一个例子:

// 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"); 

这是一个解决方案: 我们在这里不做任何改动:

function phoneticLookup(val) { 
  var result = ""; 

我们需要将switch语句转换为对象。将所有case值传输到对象属性:

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

After converting our case statements into object properties you can make use of the variable result to let the function return the correct value.


JavaScript的 result = lookup \[val\]; \`\`\`


### 资源

*   [“JavaScript对象基础” - _MDN JavaScript参考_](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics)