Files
freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-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.2 KiB
Raw Permalink Blame History

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

--description--

你可以使用前兩節課程中相同的原則來解構嵌套對象中的值。

使用與前面的例子中類似的對象:

const user = {
  johnDoe: { 
    age: 34,
    email: 'johnDoe@freeCodeCamp.com'
  }
};

這是解構對象的屬性值賦值給具有相同名字的變量:

const { johnDoe: { age, email }} = user;

這是將對象的屬性值賦值給具有不同名字的變量:

const { johnDoe: { age: userAge, email: userEmail }} = user;

--instructions--

將兩個賦值語句替換成等價的解構賦值。 lowTodayhighToday 應該爲 LOCAL_FORECASTtoday.lowtoday.high 的值。

--hints--

不能使用 ES5 的賦值語句。

assert(
  !code.match(/lowToday = LOCAL_FORECAST\.today\.low/g) &&
    !code.match(/highToday = LOCAL_FORECAST\.today.high/g)
);

應該使用解構創建 lowToday 變量。

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

應該使用解構創建 highToday 變量。

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

lowToday 應等於 64highToday 應等於 77

assert(lowToday === 64 && highToday === 77);

--seed--

--seed-contents--

const LOCAL_FORECAST = {
  yesterday: { low: 61, high: 75 },
  today: { low: 64, high: 77 },
  tomorrow: { low: 68, high: 80 }
};

// Only change code below this line

const lowToday = LOCAL_FORECAST.today.low;
const highToday = LOCAL_FORECAST.today.high;

// Only change code above this line

--solutions--

const LOCAL_FORECAST = {
  yesterday: { low: 61, high: 75 },
  today: { low: 64, high: 77 },
  tomorrow: { low: 68, high: 80 }
};

const { today: { low: lowToday, high: highToday }} = LOCAL_FORECAST;