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

267
Views
How do I loop through a nested object and subsequently add the values to HTML table cells, in Javascript?

I would like to insert parsed JSON data into a HTML table via Javascript. I think I am close to a solution, but can't figure out the final step.

Goal: I need to insert the name, age, secretIdentity and powers of three superheroes into their respective columns in a table. The data comes from a JSON file. I parsed it and it is ready to use in Javascript. I want to loop through the objects within the data and insert the correct data in the cells. I am not allowed to use jQuery or similar techniques.

Problem: The problem is that I can not seem to loop through the objects (name, age etc) for each superhero and add it to the cell. How do I target/reference the objects for use in a loop? For example, this 'works', but gives the wrong result since I don't want to loop through the names:

JavaScript code:

for(i = 0; i < members.length; i++) {
        function addRow(tableID) {
        let tableRef = document.getElementById(tableID);
        let newRow = tableRef.insertRow(-1);

        for(x = 0; x < 4; x++) {
        let newCell = newRow.insertCell(0);
        let newText = document.createTextNode(members[i].name);
        newCell.appendChild(newText);
        }

enter image description here

Instead, I want to loop through the object like this, in a horizontal way.

enter image description here

Expected solution: I expect the solution to look something like this, but it doesn't work:

for(x = 0; x < 4; x++) {
        let newCell = newRow.insertCell(0);
        let newText = document.createTextNode(members[i].obj[x]);
        newCell.appendChild(newText);

JSON data:

const data = 
  [ { squadName  : 'Super Hero Squad'
    , homeTown   : 'Metro City'
    , formed     : 2016
    , secretBase : 'Super tower'
    , active     : true
    , members : 
      [ { name           : 'Molecule Man'
        , age            : 29
        , secretIdentity : 'Dan Jukes'
        , powers         : 
          [ 'Radiation resistance'
          , 'Turning tiny'
          , 'Radiation blast'
        ] } 
      , { name           : 'Madame Uppercut'
        , age            : 39
        , secretIdentity : 'Jane Wilson'
        , powers         : 
          [ 'Million tonne punch'
          , 'Damage resistance'
          , 'Superhuman reflexes'
    ] } ] } 
  //, {}
  ]      

Is there anyone that is able to help out? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just loop through each property of object member[i]

for (i = 0; i < members.length; i++) {
    addRow('table', members[i]);
}

function addRow(tableID, member) {
    let tableRef = document.getElementById(tableID);
    let newRow = tableRef.insertRow(-1);
    for (let prop in member) {
        let newCell = newRow.insertCell(-1);
        let newText = document.createTextNode(member[prop]);
        newCell.appendChild(newText);
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

I'm not 100% sure about what columns you need but, I think this is close what you wanted. Hopefully you can see the general pattern to creating a DOM element, filling it with data and appending it to the table.

const data = 
  [ { squadName  : 'Super Hero Squad'
    , homeTown   : 'Metro City'
    , formed     : 2016
    , secretBase : 'Super tower'
    , active     : true
    , members : 
      [ { name           : 'Molecule Man'
        , age            : 29
        , secretIdentity : 'Dan Jukes'
        , powers         : 
          [ 'Radiation resistance'
          , 'Turning tiny'
          , 'Radiation blast'
        ] } 
      , { name           : 'Madame Uppercut'
        , age            : 39
        , secretIdentity : 'Jane Wilson'
        , powers         : 
          [ 'Million tonne punch'
          , 'Damage resistance'
          , 'Superhuman reflexes'
    ] } ] } 
  //, {}
  ]
  
const table = document.getElementById('root');

data.forEach(squad => {
  squad.members.forEach(hero => {
    const tr = document.createElement("tr")
    const nameTd = document.createElement("td")
    nameTd.append(hero.name)
    const ageTd = document.createElement("td")
    ageTd.append(hero.age)
    const secretTd = document.createElement("td")
    secretTd.append(hero.secretIdentity)
    const powersTd = document.createElement("td")
    powersTd.append(hero.powers.join(', '))
    
    tr.append(nameTd)
    tr.append(ageTd)
    tr.append(secretTd)
    tr.append(powersTd)
    
    table.append(tr)
  })
})
td {
  border: solid black 1px;
}
<table id="root">

</table>

about 4 years ago · Juan Pablo Isaza Report

0

What you are looking for in the nested loop, the horizontal one, is "for in". "for in" will loop through the properties of an Object. Refer to this article

https://www.w3schools.com/js/js_loop_forin.asp

But, I do not recommend you to use this method - simply access key value to generate each column (<td>), because the order of keys is not guaranteed in a loop. You still have to check the key in the nested loop to make it safe.

Does JavaScript guarantee object property order?

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!