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

291
Vistas
How to transform an array with some deep nesting into another object in order to manipulate expanding initialState in React-table

I have this data:

const data = [
  {
    "id": 1,
    "subRows": [
      {
        "id": 1,
        "subRows": [
          {
            "id": 1,
          }
        ]
      },
      {
        "id": 2,
        "subRows": [
          {
            "id": 4,
          }
        ]
      },
    ]
  },
  {
    "id": 55,
    "subRows": []
  }
];

and i need to get from this data like that, that will relay all the nesting of starter data:

const result = {
  '0': false,
  '0.0': false,
  '0.0.0': false,
  '0.1.0': false,
  '0.1.1': false,
  '1: false'
}

I need it in order to represent nesting in starting data and transform it to format, that react-table expended initial state is require. (https://react-table.tanstack.com/docs/api/useExpanded). I want to store it in local storage to prevent expanding state changing with page reload.

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

0

Take a recursive approach and ahnd over the last key as new prefix.

const
    flat = (array, prefix = '') => array.reduce((r, o, i) => {
        const key = prefix + (prefix && '.') + i;
        return {
            ...r, 
            [key]: false,
            ...flat(o.subRows || [], key)
         };
    }, {});

    data = [{ id: 1, subRows: [{ id: 1, subRows: [{ id: 1 }] }, { id: 2, subRows: [{ id: 4 }] }] }, { id: 55, subRows: [] }],
    result = flat(data);

console.log(result);

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