Object.keys() doubt

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 getArrayOfUsers(obj) {
// change code below this line
    for( let user in Object.keys(obj)){
      return Object.keys( obj[user]);
    };
// change code above this line
}

console.log(getArrayOfUsers(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0.

Challenge: Generate an Array of All Object Keys with Object.keys()

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/generate-an-array-of-all-object-keys-with-object.keys

Hi Guys,
this code isn’t correct for this challenge but i did one teste which iterate also the keys of Object although generate Error for type pass in the second iterate, someone could help ?
thanks.

Object.keys() will give an array of the keys.
for...in will also iterate through the keys of the iterabile you use it on.
So you are iterating over the keys of the Object.keys(obj) array (which are numbers)

note that the for...in loop isn’t the best to use on arrays , but anyway I don’t think this is what you wanted to do.

now, your return statement:
Object.keys() expect an object, and you are passing in obj[user], which is not an object, because obj doesn’t have property keys that are numbers, so you are passing in undefined

you are also using a return statement inside a loop, that means that it is like the loop didn’t exist, as the first iteration means the return statement is executed, and the function has an output and is exited

try looking at your code with this tool:
http://www.pythontutor.com/javascript.html#