Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Getting data from JSON and formatting this into a table

I am getting a little stuck with this one. I am trying to use javascript on my website to convert my API JSON data to a table. Without using an array in an array of data in JSON, it works fine which each item on a new line but soon as I use an nested array of data in there, it has all the data on one line, seperated by a comma. Thinking a for loop would work here but i'm not 100% sure if this is the best method. I have tried multiple searches online and tested around myself but I can't seem to get it to work.

The following is simple version of the JSON data I am working with:

MACLIST = ["ABC","DEF"]
IPLIST = ["10.10.10.10","20.20.20.20"]
ZONELIST = ["Inside","Outside"]

var json = [{"MAC":MACLIST,"IP":IPLIST,"ZONE":ZONELIST},{"SOMETHING ELSE":"OK"}];

Script used:

$(document).ready(function () {
    ko.applyBindings({
        teams: json
    });
});

HTML used:

    <table>
    <thead>
        <tr>
            <th>Device</th>
            <th>IP</th>
            <th>Zone</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: teams">
        <tr>
            <td data-bind="text: MAC"></td>
            <td data-bind="text: IP"></td>
            <td data-bind="text: ZONE"></td>
        </tr>
    </tbody>
</table>

Working JSON version (without nested array data) works:

var json = [{"MAC":"ABC","IP":"10.10.10.10","ZONE":"Inside"},{"MAC":"DEF","IP":"20.20.20.20","ZONE":"Outside}];

Any tips or advice would be greatly appreciated

Testing URL JSFiddle Link

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can map over each row to create a string of html for each row. Then inside each row, map over each column to create the string of html for each cell.

var json = [{
  "MAC": "ABC",
  "IP": "10.10.10.10",
  "ZONE": "Inside"
}, {
  "MAC": "DEF",
  "IP": "20.20.20.20",
  "ZONE": "Outside"
}];

const makeCell = (content) => {
  return `<td>${content}</td>`
}

const makeRow = (content) => {
  return `<tr>${content}</tr>`
}


const table = document.querySelector('.table')
const colHeaders = Object.keys(json[0]).map(key => makeCell(key)).join('')
const bodyRows = json.map(row => {
  return makeRow(Object.keys(row).map(col => makeCell(row[col])).join(''))
}).join('')


document.querySelector('thead tr').innerHTML = colHeaders
document.querySelector('tbody').innerHTML = bodyRows
<table>
  <thead>
    <tr></tr>
  </thead>
  <tbody>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

Hopefully this article will help you resolve the problem

https://www.geeksforgeeks.org/how-to-convert-json-data-to-a-html-table-using-javascript-jquery/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda