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

170
Vistas
How to edit and loop an Array Object in ReactJS

In my React project, I'm trying to output a table (using react-data-table-component).

So here it is my code:

import React from "react";
import DataTable from "react-data-table-component";

class WrapperTableTile extends React.Component {
  render(){
    const {data} = this.props;
    const key = this.props.data[0];
    const keys = Object.keys({...key});

    return (
      <div className="card p-4 mb-4">
        <DataTable
          title=""
          columns={keys}
          data={data}
          defaultSortField="title"
          pagination
          selectableRows
          className="table myDataTable"
          highlightOnHover={true}
        />
      </div>
    )
  }
}

WrapperTableTile.defaultProps={
  data:{
    title:"",
    columns:[],
    rows:[]
  }
}

WrapperTableTile.propTypes={
  data:WrapperTableTileType
}

export default WrapperTableTile;

My data is a JSON Array with different Objects, and I use Object.key to get only keys of the first Object, to use them as names of columns in the table.

It doesn't work because keys output is an Object like: {'ID','NAME','AGE',...} instead of DataTable needs an array to be used for columns={keys}, like:

{
  name: "ID",
  selector: "ID",
  sortable: true,
},
{
  name: "NAME",
  selector: "NAME",
  sortable: true,
},
{
  name: "AGE",
  selector: "AGE",
  sortable: true,
},

So at the end, in React I don't know how to do something like this:

for (i = 0; i < keys.length; i++) {
  {
    name: keys[i],
    selector: keys[i],
    sortable: true,
  }
}

How could I edit my array to use it as value in my component?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const keys = ['ID','NAME','AGE'].map(key=>{ name: key, selector: key, sortable: true });

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is my solution:

const tblKeys = keys.map((item) => (
  {
    name: item,
    selector: item,
    sortable: true,
  }
))

Then in my component: columns={tblKeys}

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