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.