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.chinese.md
ZhichengChen 14bfa61cea
fix(i18n): update Chinese translation of basic data structures (#38054)
Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
2020-08-04 12:44:21 +05:30

3.1 KiB

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7b7d367417b2b2512b1d Iterate Through the Keys of an Object with a for...in Statement 1 301162 使用 for...in 语句迭代对象

Description

有时候你需要遍历一个对象中的所有键。这需要 JavaScript 中的一个特殊语法:for...in 语句。以遍历 users 对象的键为例:

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

// logs:
Alan
Jeff
Sarah
Ryan

在这个语句中,我们定义了一个user变量,你可以看到,这个变量在 for...in 语句对对象的每一个键的遍历中都会被重置。 注意:
跟数组不同,对象中的键是无序的,因此一个对象中某个键的位置,或者说它出现的相对顺序,在引用或访问该键时是不确定的。

Instructions

我们已经定义了一个countOnline函数,请在其中使用一个 for...in 语句来遍历users对象中的用户,并返回online属性为true的用户的数量。

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

Tests

tests:
  - text: 函数 <code>countOnline</code> 应该使用 `for in` 语句遍历传入对象的key。
    testString: assert(code.match(/for\s*\(\s*(var|let)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)\s*{/));
  - text: '当传入 <code>{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }</code> 时,函数 <code>countOnline</code> 应该返回  <code>1</code>。'
    testString: assert(countOnline(usersObj1) === 1);
  - text: '当传入 <code>{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }</code> 时,函数 <code>countOnline</code> 应该返回  <code>2</code>。'
    testString: assert(countOnline(usersObj2) === 2);
  - text: '当传入 <code>{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }</code> 时,函数 <code>countOnline</code> 应该返回  <code>0</code>。'
    testString: assert(countOnline(usersObj3) === 0);

Challenge Seed

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

  // change code above this line
}

After Test

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
  }
}

Solution


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