What does that mean in OBJECTS? 🤔

This expression in the lesson below: Again, there will be no specific order to the entries in the array.

Basic Data Structures: Generate an Array of All Object Keys with Object.keys()

@bedward When using Object.keys() you get an array of the keys from the object. So in the lesson the object is called users. You want to use Object.keys() with a parameter to get the array of users that are the keys of the object.

And what do they mean by:

The object is called users. You have to use Object.keys() to generate an array of the users in this challenge. Objects are formatted like this `users = {

key: value

key: value
}`

Using Object.keys() will get all the keys to the object and make them an array. Hope this helps. :smile:

@bedward An array is like a list of items. If you wrote a list yourself you could alphabetize the list starting with items that start with the letter ‘A’ at the beginning. In the array that is not possible.

In most modern js interpreters, Object.keys() creates the array and arranges the keys in the order they were added to the object. But that isn’t part of any spec, there’s no requirement that they be retrieved in any particular order, simply a community standard.

What that means is, you shouldn’t expect keys to be placed in an array in alphabetic or numeric order. You can always sort that array if you needed, but by default the order is chronological.

3 Likes

Do you mean “In the Object”?

Object.keys() returns an array of the keys

but it’s actually both, do not expect a specific order either from the keys in the array or the keys in the order (like in a for…in loop)

Thank you for the explanation, very appreciated!

I didn’t understand very well what you mean by sorting the array? Can you clarify more if possible?

1 Like

an array can be sorted (with sort() method), so you can sort it in the order you need, and if you then loop through that array to access the object, the order of the elements in the array is the order in which you access the object properties

1 Like