Files
freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.md
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

2.1 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b89367417b2b2512b49 使用解構賦值從對象中分配變量 1 301215 use-destructuring-assignment-to-assign-variables-from-objects

--description--

可以給解構的值賦予一個新的變量名, 通過在賦值的時候將新的變量名放在冒號後面來實現。

還是以上個例子的對象來舉例:

const user = { name: 'John Doe', age: 34 };

這是指定新的變量名的例子:

const { name: userName, age: userAge } = user;

你可以這麼理解這段代碼:獲取 user.name 的值,將它賦給一個新的變量 userName,等等。 userName 的值將是字符串 John DoeuserAge 的值將是數字 34

--instructions--

使用解構賦值語句替換兩個賦值語句。 將 HIGH_TEMPERATUREStodaytomorrow 的值賦值給 highTodayhighTomorrow

--hints--

應該移除 ES5 賦值語句。

assert(
  !code.match(/highToday = HIGH_TEMPERATURES\.today/g) &&
    !code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)
);

應該使用解構賦值語句創建 highToday 變量。

assert(
  code.match(
    /(var|const|let)\s*{\s*(today\s*:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
  )
);

應該使用解構賦值語句創建 highTomorrow 變量。

assert(
  code.match(
    /(var|const|let)\s*{\s*(tomorrow\s*:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
  )
);

highToday 應該等於 77highTomorrow 應該等於 80

assert(highToday === 77 && highTomorrow === 80);

--seed--

--seed-contents--

const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};

// Only change code below this line

const highToday = HIGH_TEMPERATURES.today;
const highTomorrow = HIGH_TEMPERATURES.tomorrow; 

// Only change code above this line

--solutions--

const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};

const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;