freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement.md
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.8 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b7d367417b2b2512b1d 使用 for...in 语句遍历对象 1 301162 iterate-through-the-keys-of-an-object-with-a-for---in-statement

--description--

如果我们想要遍历对象中的所有属性,只需要使用 JavaScript 中的 for...in 语句即可。以遍历 users 对象的属性为例:

for (let user in users) {
  console.log(user);
}

// 输出:
Alan
Jeff
Sarah
Ryan

在上面的代码中,我们定义了一个 user 变量。可以观察到,这个变量在遍历对象的 for...in 语句执行过程中会一直被重置并赋予新值,结果就是不同的用户名打印到了 console 中。**注意:**对象中的键是无序的,这与数组不同。因此,一个对象中某个属性的位置,或者说它出现的相对顺序,在引用或访问该属性时是不确定的。

--instructions--

我们已经定义了一个 countOnline 函数,请在其中使用 for...in 语句来遍历 users 对象中的用户,并返回 online 属性为 true 的用户数量。以下是一个传入 countOnline 函数的对象示例,注意每个用户都有 online 属性,其属性值为 truefalse

{
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

--hints--

函数 countOnline 中应使用 for in 语句遍历传入的对象。

assert(
  code.match(
    /for\s*\(\s*(var|let)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)\s*{/
  )
);

当传入 { Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } } 时,函数 countOnline 应该返回 1

assert(countOnline(usersObj1) === 1);

当传入 { Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } } 时,函数 countOnline 应该返回 2

assert(countOnline(usersObj2) === 2);

当传入 { Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } } 时,函数 countOnline 应该返回 0

assert(countOnline(usersObj3) === 0);

--seed--

--after-user-code--

const usersObj1 = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

const usersObj2 = {
  Alan: {
    online: true
  },
  Jeff: {
    online: false
  },
  Sarah: {
    online: true
  }
}


const usersObj3 = {
  Alan: {
    online: false
  },
  Jeff: {
    online: false
  },
  Sarah: {
    online: false
  }
}

--seed-contents--

function countOnline(usersObj) {
  // Only change code below this line

  // Only change code above this line
}

--solutions--

function countOnline(usersObj) {
  let online = 0;
  for(let user in usersObj){
    if(usersObj[user].online) {
      online++;
    }
  }
  return online;
}