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

210
Vistas
how to set coordinates (x,y) for each id inside the tree

I have this array (of connections from ID to another):

const connectionsArr = [
  { sourceId: 1, targetId: 2 },
  { sourceId: 1, targetId: 9 },
  { sourceId: 9, targetId: 3 },
  { sourceId: 3, targetId: 5 },
  { sourceId: 3, targetId: 7 },
  { sourceId: 5, targetId: 6 },
  { sourceId: 6, targetId: 10 },
  { sourceId: 11, targetId: 12 }
];

From this array I created an array of "trees" with levels for each ID:

[
   {
      "id": 1,
      "children": [
         {
            "id": 2,
            "children": [],
            "level": 1
         },
         {
            "id": 9,
            "children": [
               {
                  "id": 3,
                  "children": [
                     {
                        "id": 5,
                        "children": [
                           {
                              "id": 6,
                              "children": [
                                 {
                                    "id": 10,
                                    "children": [],
                                    "level": 5
                                 }
                              ],
                              "level": 4
                           }
                        ],
                        "level": 3
                     },
                     {
                        "id": 7,
                        "children": [],
                        "level": 3
                     }
                  ],
                  "level": 2
               }
            ],
            "level": 1
         }
      ],
      "level": 0
   },
   {
      "id": 11,
      "children": [
         {
            "id": 12,
            "children": [],
            "level": 1
         }
      ],
      "level": 0
   }
] 

how can I set coordinates (x , y) for each id and the coordinates will be hierarchical, and finally if I decide to put them on a picture / canvas is should look like this:

image description here

I don't want to use an external library at all.

Thanks in advance to those who helped and will help .

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