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

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

#1

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

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]


#4

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


#5

@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:


#6

@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


#7

@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!


#8

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


#9

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?


#10

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

#11

Thanks for the explanation!


#12

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));

#13

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


#14

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
}

#15

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


#16

why the online is a string?