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

Generate an Array of All Object Keys with Object.keys() 1
0

#1

Tell us what’s happening:
j
don’t get it

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

  // change code above this line
}

console.log(getArrayOfUsers(users));

Your browser information:

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

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


#2

Basically the solution is given for you in the instructions where it says,

Object.keys()

You will want to return keys of any object (object parameter) that is passed within the function.

return Object.keys(obj);


#3

HI I am still having problem with my code with the example you gave @shimphillip it states that object is undefined. What am I doing wrong cause I write return object.keys(obj); but object is said to be undefined.


#4

It’s capital O in object.


#5

In order to get keys of any object the syntax is Object.keys(<object_name>). This returns an array.

In this challenge we have a nested Object,meaning we have keys which also contains objects and we need to list them all.

Here is the function that I wrote to get all the keys:

function getArrayOfUsers(obj) {
// change code below this line
let newArr = [];
for(let user in obj){
newArr.push(user); //Here I am also adding the first level keys (Alan, Jeff, Sarah and Ryan with each iteration.
newArr.push((Object.keys(obj[user]))); //Here I am adding the keys of Alan, Jeff, Sarah and Ryan with each iteration
}

return newArr; //Returning the array to calling function.

// change code above this line
}


#6

I’m glad you explained that, @shimphillip!

My brain just said “No, it’s not going to be that simple,” and then just said what the heck are you supposed to do here?! Haha, I’ve got to stop overestimating a challenge on here.