In JavaScript, we can iterate through the object's properties using the following methods:
for...in loop:This loop iterates over all the object's properties using the following syntax:
for (const obj in Object) {// body}
Let's see an example of the for..in loop in the code snippet below:
// create an objectconst data = {name: 'Shubham',designation: 'Software Engineer'}// iterate over objectfor (const obj in data) {// print key and value on consoleconsole.log(`${obj}: ${data[obj]}`)}
data.for...in loop to iterate over the properties of the object data.data on the console.object.entries() method:This method returns an array of the object's properties in [key, value] format. While using the for...of loop, we can iterate over it using the following syntax.
for (const [key, value] in Object.entries(objectName)) {// body}
Let's see an example of the object.entries() method in the code snippet below:
// create an objectconst data = {name: 'Shubham',designation: 'Software Engineer'}// iterate over objectfor (const [key, value] of Object.entries(data)) {// print key and value on consoleconsole.log(`${key}: ${value}`)}
data.Object.entries() method to iterate over the properties of data.data to the console.