Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda