bradxr
8/5/2018 - 6:54 AM

Iterate Through the Keys of an Object with a for...in Statement

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

// logs:
Alan
Jeff
Sarah
Ryan




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) {
  let count = 0;
  
  for (let user in obj) {
    if(obj[user]['online'] === true) {
      count++;
    }
  }

  return count;
}

console.log(countOnline(users));