Files
freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-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
5cfa550e84205a357704ccb6 使用解構賦值來獲取對象的值 1 301216 use-destructuring-assignment-to-extract-values-from-objects

--description--

解構賦值是 ES6 引入的新語法,用來從數組和對象中提取值,並優雅地對變量進行賦值。

有如下 ES5 代碼:

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

const name = user.name;
const age = user.age;

name 的值應該是字符串 John Doe age 的值應該是數字 34

下面是使用 ES6 解構賦值語句,實現相同效果:

const { name, age } = user;

同樣,name 的值應該是字符串 John Doe age 的值應該是數字 34

在這裏,自動創建 nameage 變量,並將 user 對象相應屬性的值賦值給它們。 這個方法簡潔多了。

你可以從對象中提取儘可能多或很少的值。

--instructions--

把兩個賦值語句替換成效果相同的解構賦值語句。 todaytomorrow 的值應該還是 HIGH_TEMPERATURES 對象中 todaytomorrow 屬性的值。

--hints--

應該移除 ES5 賦值語句。

assert(
  !code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
);

應該使用解構賦值創建 today 變量。

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

應該使用解構賦值創建 tomorrow 變量。

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

today 應該等於 77tomorrow 應該等於 80

assert(today === 77 && tomorrow === 80);

--seed--

--seed-contents--

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

// Only change code below this line

const today = HIGH_TEMPERATURES.today;
const tomorrow = HIGH_TEMPERATURES.tomorrow;

// Only change code above this line

--solutions--

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

const { today, tomorrow } = HIGH_TEMPERATURES;