JavaScript: getting keys of object

In this tutorial we will learn how to obtain the keys of a JavaScript object, using a method from the Object class.

Introduction

In this tutorial we will learn how to obtain the keys of a JavaScript object, using a method from the Object class. The keys will be obtained as an array of strings, where each string contains the name of the property.

Note that through the tutorial we are going to use the terms “keys” and “properties” interchangeably.

It’s important to mention that this method will allow us only to obtain the keys at the root of the object, not the nested ones. Nonetheless, there are also ways of obtaining nested properties, depending on the needs of the application. One of the possible approaches might be flattening the properties of the JSON (like seen here) and then using the method shown on this tutorial to iterate over the results. We are not going to cover these more advanced procedures here.

The code

We will start by defining an object with 3 properties, for illustration purposes. Note that one of the properties will correspond to another object with a nested property, to show that we are only going to obtain the properties at the root of our object.

const obj = {
    property1: "some value",
    property2: 10,
    property3: {
        nestedProperty: 20
    }

}

Now, to obtain an array with strings containing the names of the properties of the object, we simply need to call the keys method of the Object class, passing as input our object.

const properties = Object.keys(obj);
console.log(properties);

Since we have the names of the properties, if we want to obtain their corresponding values we can simply iterate through the obtained array and access the object using those keys, like shown below.

for(let i = 0; i<properties.length; i++){
    console.log(obj[properties[i]])
}

Note that, as an alternative to obtain the values of the object, we can use the values method of the Object class.

The complete code can be seen below.

const obj = {
    property1: "some value",
    property2: 10,
    property3: {
        nestedProperty: 20
    }

}

const properties = Object.keys(obj);

console.log("Properties:");
console.log(properties);

console.log("\n\nValues:");

for(let i = 0; i<properties.length; i++){
    console.log(obj[properties[i]])
}

Testing the code

To test the code, simply run it in a tool of your choice. I’ve used Visual Studio Code with the Code Runner extension and node.js version 14.3.0.

You should get an output similar to figure 1. As can be seen, we obtain an array containing the keys, as strings, and then all the properties. Like expected, the nested property doesn’t get listed in the array as a different approach would be needed to also obtain the nested ones.

Output of the program on Visual Studio Code
Figure 1 – Output of the program on Visual Studio Code.

2 Replies to “JavaScript: getting keys of object”

  1. I was trying to figure out what “\n ” was, I guess it’s just a new line, I think I remember that the \ in a string is an escape character. This is a great post I’m working on the same stuff with basic JS Objects behavior.

Leave a Reply