Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Iterate Through the Keys of an Object

// 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));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Iterate #Through #Keys #Object
ADD COMMENT
Topic
Name
6+8 =