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

206
Vistas
How to tree a array as hierarchy without to know the level limit

I have a list template like this

list = [
    {
      id: "G1",
      text: "Group 1",
      idparent: "",
      children: [
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "1",
          text: "Task 1"
          idparent: "G1",
        },
        {
          start: "2014-10-03",
          end: "2014-10-05",
          id: "2",
          text: "Task 2"
          idparent: "G1",
        }    
      ]
    },
    {
      start: "2014-10-01",
      end: "2014-10-03",
      id: "3",
      text: "Task 3"
      idparent: "",
    },    
  ];

The first item with children is a group of items and the last one is a single item. And the list to be treated is the following

list_to_tree = [
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "G1",
          text: "Group 1"
          id_parent: ""
        },   
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "1",
          text: "Task 1"
          id_parent: "G1"
        },   
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "2",
          text: "Task 2",
          id_parent: "G1"
        },   
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "3",
          text: "Task 3"
          id_parent: ""
        },   
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "G2",
          text: "Group 2"
          id_parent: "G1"
        },
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "4",
          text: "Task 4",
          id_parent: "G2"
        },
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "5",
          text: "Task 5",
          id_parent: ""
        },
        {
          start: "2014-10-01",
          end: "2014-10-03",
          id: "G3",
          text: "Group 3",
          id_parent: ""
        },
   ];

How to sort this list so that it can look like the model above knowing that we can have an infinite number(n) of child spots (group or item). Eg: Task -> subtasks -> n(substasks) if have

about 4 years ago · Juan Pablo Isaza
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