M
M
Muhammad Magomedov2021-06-25 20:44:51
JavaScript
Muhammad Magomedov, 2021-06-25 20:44:51

How to convert such a nested object?

Hello.

I have this nested object:

[
  {
    ID: 1,
    NAME: 'Телефоны и гаджеты',
    CHILDREN: [
      {
        ID: 2,
        NAME: 'Смартфоны',
        CHILDREN: [
          {
            ID: 2,
            NAME: 'Смартфоны iPhone'
          }
        ]
      }
    ]
  }
]


And I want to convert it to this:
{
  id: 1,
  name: 'Телефоны и гаджеты',
  children: {
    id: 2,
    name: 'Смартфоны',
    children: {
      id: 2,
      name: 'Смартфоны iPhone'
    }
  }
}

No matter how much you try, it just doesn't work.

Can you please tell me how to write a correct function that will go through all the objects and return the desired structure?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Klein Maximus, 2021-06-25
@Web_Mukhammad

For example, using recursion - https://codesandbox.io/s/pensive-kapitsa-lthws?fil...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question