Looping through an object to get its properties and values

919 0 0 0

Last Updated : 2024-04-29 04:24:26

In case you have an object and need to loop through all its keys or properties and its related values you can use the following syntax

const movie = {
info: {
title: "Eagle Eye",
level : 5,
type: 'action'
},
id: Math.random()
}

for(const key in movie.info) {
console.log(`${key} : ${movie.info[key]}`);
}

//The output would be :
// title : Eagle Eye
// level : 5
// type : action

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .