Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords.chinese.md
complete-a-promise-with-resolve-and-reject.chinese.md
create-a-javascript-promise.chinese.md
create-a-module-script.chinese.md
create-an-export-fallback-with-export-default.chinese.md
create-strings-using-template-literals.chinese.md
declare-a-read-only-variable-with-the-const-keyword.chinese.md
explore-differences-between-the-var-and-let-keywords.chinese.md
handle-a-fulfilled-promise-with-then.chinese.md
handle-a-rejected-promise-with-catch.chinese.md
import-a-default-export.chinese.md
mutate-an-array-declared-with-const.chinese.md
prevent-object-mutation.chinese.md
reuse-javascript-code-using-import.chinese.md
set-default-parameters-for-your-functions.chinese.md
use--to-import-everything-from-a-file.chinese.md
use-arrow-functions-to-write-concise-anonymous-functions.chinese.md
use-class-syntax-to-define-a-constructor-function.chinese.md
use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md
use-destructuring-assignment-to-assign-variables-from-objects.chinese.md
use-destructuring-assignment-to-extract-values-from-objects.chinese.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md
use-export-to-share-a-code-block.chinese.md
use-getters-and-setters-to-control-access-to-an-object.chinese.md
use-the-rest-parameter-with-function-parameters.chinese.md
use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
write-arrow-functions-with-parameters.chinese.md
write-concise-declarative-functions-with-es6.chinese.md
write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md
write-concise-object-literal-declarations-using-simple-fields.chinese.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md
2020-08-04 12:43:35 +05:30

104 lines
2.5 KiB
Markdown

---
id: 587d7b89367417b2b2512b4a
title: Use Destructuring Assignment to Assign Variables from Nested Objects
challengeType: 1
forumTopicId: 301214
localeTitle: 使用解构赋值从嵌套对象中分配变量
---
## Description
<section id='description'>
同样,我们可以将 <em>嵌套的对象</em>解构到变量中。
请看以下代码:
```js
const user = {
johnDoe: {
age: 34,
email: 'johnDoe@freeCodeCamp.com'
}
};
```
这是解构对象的属性并赋值给相同名字的变量:
```js
const { johnDoe: { age, email }} = user;
```
这是将对象的属性值指定给一个不同的名字:
```js
const { johnDoe: { age: userAge, email: userEmail }} = user;
```
</section>
## Instructions
<section id='instructions'>
将两个赋值语句替换成等价的解构赋值。<code>lowToday</code><code>highToday</code> 应该为 <code>LOCAL_FORECAST</code><code>today.low</code><code>today.high</code> 的值。
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: 不能使用 ES5 的赋值语句。
testString: assert(!code.match(/lowToday = LOCAL_FORECAST\.today\.low/g) && !code.match(/highToday = LOCAL_FORECAST\.today.high/g))
- text: 应该使用解构创建 <code>lowToday</code> 变量。
testString: 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));
- text: 应该使用解构创建 <code>highToday</code> 变量。
testString: 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));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
const LOCAL_FORECAST = {
yesterday: { low: 61, high: 75 },
today: { low: 64, high: 77 },
tomorrow: { low: 68, high: 80 }
};
// change code below this line
const lowToday = LOCAL_FORECAST.today.low;
const highToday = LOCAL_FORECAST.today.high;
// change code above this line
console.log(lowToday); // should be 64
console.log(highToday); // should be 77
```
</div>
</section>
## Solution
<section id='solution'>
```js
const LOCAL_FORECAST = {
yesterday: { low: 61, high: 75 },
today: { low: 64, high: 77 },
tomorrow: { low: 68, high: 80 }
};
// change code below this line
const { today: { low: lowToday, high: highToday }} = LOCAL_FORECAST;
// change code above this line
console.log(highToday); // should be 77
console.log(highTomorrow); // should be 80
```
</section>