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

82
Visualizações
Can't get mapped array of objects to show in HTML table React

I can't figure out what could be wrong i'm trying to map data that seems like this

[
  RowDataPacket { id: 1, words: 'one', numbers: 1 },
  RowDataPacket { id: 2, words: 'two', numbers: 2 }
]

into a table but it ends up showing nothing.

i have tried even with forEach but the same result, really if someone help me out with this one i would really appreciate it.

function TableCrud ({dbIo, isLoading, dbData}){
  async function triggerFetch() {
    const dbData = await dbIo('getData')
    console.log(dbData + ' dbData awaited inside TableCrud')// Console.log
  }

  function insertRow(data) {
    if (isLoading) {
      triggerFetch()
      return (
        <tr>
          <td id='no-data' colSpan='3'>Wait...</td>
        </tr>
      )
    }
    else if(data.length === 0) {
      return (
        <tr>
          <td id='no-data' colSpan='3'>Nothing to Display</td>
        </tr>
      )
    } else {
      data.map((item, index) => {
        return (
          <tr key={index}>
            <td>{item.words}</td>
            <td>{item.numbers}</td>
            <td><input className='input-check' id={'select-'+index} type="checkbox"/></td>
          </tr>
        )
      })
    }
  }

  return (
    <div id='table-data'>
      <table id='table'>
        <thead>
          <tr>
            <th>Word</th>
            <th>Number</th>
            <th>Selected</th>
          </tr>
        </thead>
        <tbody id='table-body'>
          {insertRow(dbData)}
        </tbody>
      </table>
    </div>
  )
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can try something like this

if(!data){
    return (
    <tr>
        <td id='no-data' colSpan='3'>Nothing to Display</td>
    </tr>
 )else if(data.length !== 0){
 data.map((item, index) => {
        return (
          <tr key={index}>
            <td>{item.words}</td>
            <td>{item.numbers}</td>
            <td><input className='input-check' id={'select-'+index} type="checkbox"/></td>
          </tr>
        )
      })
}
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think your sample isn't valid JSON:

[
  RowDataPacket { id: 1, words: 'one', numbers: 1 },
  RowDataPacket { id: 2, words: 'two', numbers: 2 }
]

It would need to look like this to work with your map:

[
  { id: 1, words: 'one', numbers: 1 },
  { id: 2, words: 'two', numbers: 2 }
]
about 4 years ago · Juan Pablo Isaza Relatório

0

Missed return sentence. It should be like:

 return data.map((item, index) => {
        return (
          <tr key={index}>
            <td>{item.words}</td>
            <td>{item.numbers}</td>
            <td><input className='input-check' id={'select-'+index} type="checkbox"/></td>
          </tr>
        )
      })

So the function can return the result of map(). :)

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