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

Tell us what’s happening:

Your code so far


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
  var index=0;
  for(let o in obj){
      if(o.online == true){
       index++;
      }
  }
return index;
// change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement/

1 Like

Try using console.log to see what o Is - hint: it is a string

Yes !!! That’s a string

A string doesn’t have a property online

how to iterate over the name?

function countOnline(obj) {
  // change code below this line
  var count = 0;
  for (var key in obj) {
    if (obj[key].online == true) {
      count++;
    }
  }
  return count;

  // change code above this line
}
1 Like

Awesome that you solved it, but please don’t post full working solution, to avoid spoiling things to other users

1 Like

Thanks for the hint helped me . Thanks again

1 Like