Files
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups.md
2022-11-18 13:12:24 -08:00

3.1 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244ca 使用对象进行查找 1 https://scrimba.com/c/cdBk8sM 18373 using-objects-for-lookups

--description--

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

Here is an example of an article object:

const article = {
  "title": "How to create objects in JavaScript",
  "link": "https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-in-javascript-b0e2450655e8/",
  "author": "Kaashan Hussain",
  "language": "JavaScript",
  "tags": "TECHNOLOGY",
  "createdAt": "NOVEMBER 28, 2018"
};

const articleAuthor = article["author"];
const articleLink = article["link"];

const value = "title";
const valueLookup = article[value];

articleAuthor is the string Kaashan Hussain, articleLink is the string https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-in-javascript-b0e2450655e8/, and valueLookup is the string How to create objects in JavaScript.

--instructions--

把 switch 语句转化为对象 lookup 调用。 使用它来查找 val 属性的值,并赋值给 result 变量。

--hints--

phoneticLookup("alpha") 应该等于 Adams

assert(phoneticLookup('alpha') === 'Adams');

phoneticLookup("bravo") 应该等于 Boston

assert(phoneticLookup('bravo') === 'Boston');

phoneticLookup("charlie") 应该等于 Chicago

assert(phoneticLookup('charlie') === 'Chicago');

phoneticLookup("delta") 应该等于 Denver

assert(phoneticLookup('delta') === 'Denver');

phoneticLookup("echo") 应该等于 Easy

assert(phoneticLookup('echo') === 'Easy');

phoneticLookup("foxtrot") 应该等于字符串 Frank

assert(phoneticLookup('foxtrot') === 'Frank');

phoneticLookup("") 应该等于 undefined

assert(typeof phoneticLookup('') === 'undefined');

请不要修改 return 语句

assert(code.match(/return\sresult;/));

请不要使用 caseswitchif 语句

assert(
  !/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g, ''))
);

--seed--

--seed-contents--

// Setup
function phoneticLookup(val) {
  let 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;
}

phoneticLookup("charlie");

--solutions--

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

  const lookup = {
    alpha: "Adams",
    bravo: "Boston",
    charlie: "Chicago",
    delta: "Denver",
    echo: "Easy",
    foxtrot: "Frank"
  };

  result = lookup[val];

  return result;
}