Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.2 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b7c367417b2b2512b19 修改嵌套在对象中的对象 1 301164 modify-an-object-nested-within-an-object

--description--

现在我们来看一个稍复杂的对象。在对象中,我们也可以嵌套任意层数的对象,对象的属性值可以是 JavaScript 支持的任意类型,包括数组和其他对象。请看以下例子:

let nestedObject = {
  id: 28802695164,
  date: 'December 31, 2016',
  data: {
    totalUsers: 99,
    online: 80,
    onlineStatus: {
      active: 67,
      away: 13
    }
  }
};

nestedObject 有 3 个属性:id(属性值为数字)、date(属性值为字符串)、data(属性值为嵌套的对象)。虽然对象中的数据可能很复杂,我们仍能使用上一个挑战中讲到的写法来访问我们需要的信息。如果我们想把嵌套在 onlineStatusbusy 的属性值改为 10,可以用点号表示法来这样实现:

nestedObject.data.onlineStatus.busy = 10;

--instructions--

我们已经定义了一个 userActivity 对象,它包含了另一个对象。请将 online 的属性值改为 45

--hints--

userActivity 应包含 iddatedata 属性。

assert(
  'id' in userActivity && 'date' in userActivity && 'data' in userActivity
);

userActivity 应包含 data 属性,其属性值应为包含 totalUsersonline 属性的对象。

assert('totalUsers' in userActivity.data && 'online' in userActivity.data);

userActivitydata 属性值中的 online 属性值应被改为 45

assert(userActivity.data.online === 45);

应使用点号表示法或方括号表示法来修改 online 属性值。

assert.strictEqual(code.search(/online: 45/), -1);

--seed--

--seed-contents--

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

// Only change code below this line

// Only change code above this line

console.log(userActivity);

--solutions--

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

userActivity.data.online = 45;