Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Iterate over the Properties of an object and its childs objects

How can I iterate over the Properties of an object and its childs objects? I used for in but I can't get the Songs info :/

let discos = [];

let disco1 = {
    discoName: 'name disco1',
    band: 'band name',
    code: 1,
    songs: [
        { 'songName': 'Song Name',
          'duration': 200,
       },
    ],
};
let disco2 = {
    discoName: 'name disco 2',
    band: 'band name 2',
    code: 1,
    songs: [
        { 'songName': 'Song Name 0',
          'duration': 200,
       },
    ],
};

discos.push(disco1,disco2);

for (let disco in discos){
    console.log(discos[disco].discoName);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want a truly recursive solution, you will need to use a recursion, since this cannot be done using for.. in:

let discos = [];

let disco1 = {
    discoName: 'name disco1',
    band: 'band name',
    code: 1,
    songs: [
        { 'songName': 'Song Name',
          'duration': 200,
       },
    ],
};
let disco2 = {
    discoName: 'name disco 2',
    band: 'band name 2',
    code: 1,
    songs: [
        { 'songName': 'Song Name 0',
          'duration': 200,
       },
    ],
};

discos.push(disco1,disco2);

const visitRec = (obj, visitor) => {
  if (obj instanceof Array) {
    obj.forEach(e => visitRec(e, visitor));
  } else if (typeof obj == 'object') {
    for (let key in obj) {
      visitRec(obj[key], visitor);
    }
  } else {
    visitor(obj);
  }
}

visitRec(discos, console.log);

However, what I think you want is just:

for (let disco of discos){
  console.log(disco.discoName);
  for (let song of disco.songs) {
    console.log(song.songName);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!