In JavaScript, What Is The Method For Iterating Through An Ordinary Object With Its Properties As Members?

If the JavaScript object contains other objects as its members, you can loop through the object using a for…in loop. Here is an example:

const myObject = {
  key1: 'value1',
  key2: {
    subKey1: 'subValue1',
    subKey2: 'subValue2'
  key3: 'value3'

for (let key in myObject) {
  // Check if the member is an object
  if (typeof myObject[key] === 'object' && myObject[key] !== null) {
    // Loop through the sub-object
    for (let subKey in myObject[key]) {
      console.log(`myObject[${key}][${subKey}] = ${myObject[key][subKey]}`);
  } else {
    console.log(`myObject[${key}] = ${myObject[key]}`);

In this example, the outer loop iterates over each member of the object. Inside the loop, it checks if the member is an object using typeof myObject[key] === 'object' && myObject[key] !== null. If it is an object, it then loops through the sub-object using another for…in loop. Otherwise, it simply prints the value.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!