Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!