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

Tell us what’s happening:

can anybody explain to me why this isn’t working? Thanks

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

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 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

2 Likes

Hi,

I believe it’s because of the way you have the object key and then another object as it’s property, therefore the online key is buried another level down you would need to know user.Alan.online or something similar.

[Edited for the ubiquitous typo]

3 Likes

Hi
Quoted from the challenge

Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a for…in statement.

Your code only works if user is an object.
Try console.log(user) to see what user really is.

function countOnline(obj) {
    // change code below this line
    let counter = 0;
    for (let user in obj) {
        console.log(user);  // not what you think!
        if(user.online === true){
            counter ++;
        }
    }
    return counter;

    // change code above this line
}

The solution provided by @T-Minus14 is probably a better real world solution but to satisfy this challenge you must use for…in

6 Likes

@alhazen1 I withdrew the code when I realised the structure was challenge dependant. You have hinted at the solution much better than I did or could :slight_smile:

2 Likes

@cjbechtl your “if” statement has the wrong call to the online property of the users object. see the solution below and notice the if statement

function countOnline(obj) {
    // change code below this line
    var counter = 0;
    for (var user in obj) {
        if (obj[user]["online"] === true) {
            counter++
        }
        // change code above this line

    } return counter;
}
console.log(countOnline(newUsers));

use bracket notation to access the online property

47 Likes

@alhazen1 thank you for catching that. I guess it’s difficult to access an “objects’s” properties when it’s actually a string :joy:

@chinonsoebere i didn’t think to try bracket notation, thanks!

2 Likes

I think that is wrong.

I’m not an expert but I think your if statement is wrong because it can’t know which object you’re refering to so I think that the problem is not in bracket or dot, it’s that you forgot to include obj as the source.
It has to be: obj[user].online

2 Likes

obj[user].online

and

obj.user.online
lead to a different result of the challenge.

is it that if it’s uncertain property name you can only use the bracket method to access the data?

6 Likes

Exactly. If you are using a variable property name then you must use bracket notation.

const obj = {name:"Sarah",age:23,role:"Zombie Slayer"};
const job = "role";

console.log(obj.role); // this works
console.log(obj["role"]);  // this also works
console.log(obj[job]);  // this also works

console.log(obj.job);  // this does not work
18 Likes

Thanks for the explanation!

Not until I changed the condition from " user.online" to “obj[user].online my code didn’t work.”

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
  let onlineUsers = 0;
  for(let user in obj){
    if(obj[user].online === true){
      onlineUsers = onlineUsers + 1;
    }
  }
  return onlineUsers;
  // change code above this line
}

console.log(countOnline(users));
1 Like

The reason the code wasn’t working is because you can only access variables with bracket notation. In this case, ‘user’ was a variable.

4 Likes

You can access an object’s property either with the dot or bracket notation. The reason the dot notation doesn’t work in this particular case is that if you try to run the code below you will see that the typeof operator returns a string and not an actual object.

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
  let logged = 0;
  for(let user in obj)
  {
     /*if (obj[user].online)
    {
      logged++;
    } */ 

    console.log(typeof user);
  }

  return logged;
  // change code above this line
}
2 Likes

The user is a var, so change the user.online to obj[user].online

2 Likes

why the online is a string?

This should be the top-rated comment. This explains why my solution (as it turns out, the exact same as OP before I came upon this post) didn’t work, and it shows how using variables to access properties needs to look in order to finish the challenge. Thank you so much!

1 Like

Hi !

Just a little bit of advice.

It’s better and good practice to access Object Property using the dote notation obj[user].online when you know exactly its name. In this case online.

Thank you!

Hope this helps someone.

1 Like

Hi @Oliver_Olivier,

Good question. In this situation, the better way to access the online property is using the dote . notation as follow obj[user].online.

Hope this helps.

Thanks for this. By the way, can you explain why 've not used users in place of user in obj[user]["online"]?

Why do you have to write counter++?