Categories
JavaScript Answers

How to iterate through property names of JavaScript object?

Spread the love

Sometimes, we want to iterate through property names of JavaScript object.

In this article, we’ll look at how to iterate through property names of JavaScript object.

How to iterate through property names of JavaScript object?

To iterate through property names of JavaScript object, we can use the Object.keys method.

For instance, we write

Object.keys(obj).forEach((key) => {
  console.log(key);
});

to call Object.keys with obj to return an array of string property keys in obj.

Then we call forEach with a function that logs the key, which is the property name string to loop through each element returned by Object.keys.

Conclusion

To iterate through property names of JavaScript object, we can use the Object.keys method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *