freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-data-structures/iterate-through-the-keys-of...

2.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b7d367417b2b2512b1d Iterate Through the Keys of an Object with a for...in Statement 1 使用for ... in Statement中的对象键迭代

Description

有时您可能需要遍历对象中的所有键。这需要JavaScript中的特定语法称为for ... in语句。对于我们的users对象,这可能看起来像:
for让用户在用户中{
的console.log用户;
};

//日志:
艾伦
杰夫
莎拉
瑞安
在这个语句中,我们定义了一个变量user ,正如您所看到的,在每次迭代期间,当该语句循环遍历该对象时,该变量被重置为每个对象的键,从而导致每个用户的名称被打印到控制台。 注意:
对象不像数组那样保持对存储键的排序;因此,当引用或访问该密钥时,对象上的键位置或其出现的相对顺序是无关紧要的。

Instructions

我们定义了一个函数countOnline ;在此函数中使用for ... in语句循环访问users对象中的users并返回其online属性设置为true的用户数。

Tests

tests:
  - text: <code>users</code>对象包含用户<code>Jeff</code>和<code>Ryan</code>  <code>online</code>设置为<code>true</code> ,用户<code>Alan</code>和<code>Sarah</code> <code>online</code>设置为<code>false</code>
    testString: 'assert(users.Alan.online === false && users.Jeff.online === true &&  users.Sarah.online === false &&  users.Ryan.online === true, "The <code>users</code> object contains users <code>Jeff</code> and <code>Ryan</code> with <code>online</code> set to <code>true</code> and users <code>Alan</code> and <code>Sarah</code> with <code>online</code> set to <code>false</code>");'
  - text: 函数<code>countOnline</code>返回<code>online</code>属性设置为<code>true</code>的用户数
    testString: 'assert((function() { users.Harry = {online: true}; users.Sam = {online: true}; users.Carl = {online: true}; return countOnline(users) })() === 5, "The function <code>countOnline</code> returns the number of users with the <code>online</code> property set to <code>true</code>");'

Challenge Seed

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line

  // change code above this line
}

console.log(countOnline(users));

Solution

// solution required