Javascript
JavaScript Fundamentals -- Objects in JavaScript

Working with the Object's Keys

PRO
Outline

Sometimes you will need to do something with the keys on an object. Maybe you want to output what information is available on the object, or you want to create a new object with the same data but changing the key names. Having access to what those keys are would be really helpful.

The Object class has a keys method that takes an object as its lone argument. The return value is an array with all the keys on the object.

const dog = {
  name: 'Duke',
    age: 3
}

console.log(Object.keys(dog)); // ['name', 'age']

Once you have the array of keys, you can do whatever you'd like, including looping over the keys in a for .. of loop.

Click here to read more about working with object keys.

 

I finished! On to the next chapter