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
-iterate-through-the-keys-of-an-object-with-a-for...in-statement.chinese.md
access-an-arrays-contents-using-bracket-notation.chinese.md
access-property-names-with-bracket-notation.chinese.md
add-items-to-an-array-with-push-and-unshift.chinese.md
add-items-using-splice.chinese.md
add-key-value-pairs-to-javascript-objects.chinese.md
check-for-the-presence-of-an-element-with-indexof.chinese.md
check-if-an-object-has-a-property.chinese.md
combine-arrays-with-the-spread-operator.chinese.md
copy-an-array-with-the-spread-operator.chinese.md
copy-array-items-using-slice.chinese.md
create-complex-multi-dimensional-arrays.chinese.md
generate-an-array-of-all-object-keys-with-object.keys.chinese.md
iterate-through-all-an-arrays-items-using-for-loops.chinese.md
modify-an-array-stored-in-an-object.chinese.md
modify-an-object-nested-within-an-object.chinese.md
remove-items-from-an-array-with-pop-and-shift.chinese.md
remove-items-using-splice.chinese.md
use-an-array-to-store-a-collection-of-data.chinese.md
use-the-delete-keyword-to-remove-object-properties.chinese.md
basic-javascript
debugging
es6
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
formattingConversion
math-challenges
requiresTests
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
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-data-structures/modify-an-object-nested-within-an-object.chinese.md

2.9 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b7c367417b2b2512b19 Modify an Object Nested Within an Object 1 修改嵌套在对象中的对象

Description

现在让我们来看一个稍微复杂的对象。对象属性可以嵌套到任意深度它们的值可以是JavaScript支持的任何类型的数据包括数组甚至其他对象。考虑以下
让nestedObject = {
id28802695164
日期:'2016年12月31日'
数据:{
总用户99
在线80
onlineStatus{
活跃67
离开13
}
}
};
nestedObject有三个唯一的键: id ,其值为数字, date为字符串的data data ,其值为另一个嵌套在其中的对象。虽然结构很快就会变得复杂,但我们仍然可以使用相同的符号来访问我们需要的信息。

Instructions

在这里,我们定义了一个对象userActivity ,其中包含嵌套在其中的另一个对象。您可以像修改上一个挑战中的属性一样修改此嵌套对象的属性。将online密钥的值设置为45

Tests

tests:
  - text: <code>userActivity</code>具有<code>id</code>  <code>date</code>和<code>data</code>属性
    testString: 'assert("id" in userActivity && "date" in userActivity && "data" in userActivity, "<code>userActivity</code> has <code>id</code>, <code>date</code> and <code>data</code> properties");'
  - text: <code>userActivity</code>具有设置为具有密钥<code>totalUsers</code>和<code>online</code>的对象的<code>data</code>密钥
    testString: 'assert("totalUsers" in userActivity.data && "online" in userActivity.data, "<code>userActivity</code> has a <code>data</code> key set to an object with keys <code>totalUsers</code> and <code>online</code>");'
  - text: 嵌套在<code>userActivity</code> <code>data</code>键中的<code>online</code>属性应设置为<code>45</code>
    testString: 'assert(userActivity.data.online === 45, "The <code>online</code> property nested in the <code>data</code> key of <code>userActivity</code> should be set to <code>45</code>");'
  - text: <code>online</code>属性使用点或括号表示法设置
    testString: 'assert.strictEqual(code.search(/online: 45/), -1, "The <code>online</code> property is set using dot or bracket notation");'

Challenge Seed

let userActivity = {
  id: 23894201352,
  date: 'January 1, 2017',
  data: {
    totalUsers: 51,
    online: 42
  }
};

// change code below this line

// change code above this line

console.log(userActivity);

Solution

// solution required