Using Objects for Lookups之js:使用对象查找(强行翻译)

If you have tabular data[表格数据], you can use an object to "lookup" values rather than a switch statement or an if/else chain. 

当使用表格数据时用对象进行查找value

要求:Convert the switch statement into an object called lookup. Use it to look up val and assign the associated string to the result variable.

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

//test
console.log(phoneticLookup("charlie"));
val];
  return result;
}

//test
console.log(phoneticLookup("charlie"));
 

猜你喜欢

转载自blog.csdn.net/qq_40642021/article/details/80812675