📜  遍历对象的键 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:17.627000             🧑  作者: Mango

代码示例3
// Iterate Through the Keys of an Object

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

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

console.log(countOnline(usersObj));