Javascript Object Values & Object Keys
The Javascript Object has some methods available on it that make it easier to iterate over it’s properties or it’s values. Object.values which was introduced by ES2017 outputs values of a javascript object into an array and Object.keys outputs the keys as an array.
In the above students (likely graduate students 😏) we can output just the ages of the students by using Object.values(). We can conversely output just the names of the students by using Object.keys(). This can be useful if want to order the students alphabetically or order them by age, for example:
Let’s order them by age:
If for some reason you get an error message that says Object.values isn’t available, you can also use Object.map to obtain the same effect.
Lastly, there is the Object.entries which returns an array of arrays of each key and value:
- Read more about Object.values()
- Read more about Object.keys()
- Read more about Object.entries()