N
N
Neuro2020-04-15 12:14:06
JavaScript
Neuro, 2020-04-15 12:14:06

How to pass data from children to parent in a tree object?

Good afternoon, there is such an object in the array

[
  {
    "id": 1,
    "parentid": null,
    "count": "0",
    "children": [
      {
        "id": 2,
        "parentid": 1,
        "count": "89",
        "children": [
          {
            "id": 3,
            "parentid": 2,
            "count": "19",
            "children": [
              {
                "id": 5,
                "parentid": 3,
                "count": "205"
              },
              {
                "id": 4,
                "parentid": 3,
                "count": "8"
              }
            ]
          }
        ]
      }
    ]
  }
]

I need to pass the count value from the children to each parent above, does anyone know the solution?
5e96d05806566003960413.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-04-15
@Rsa97

Recursively traverse the entire tree in depth and calculate for each element the data on its descendants.

M
mayton2019, 2020-04-15
@mayton2019

At the same time, the original tree should remain unchanged?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question