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

129
Vistas
How can I turn this array structure into this other pattern?

From this:

[["a", "b"], ["a", "c"], ["a", "d"], ["d", "e"], ["d", "f"], ["f", "g"]]

To this:

[["a", "b"], ["a", "c"], ["a", "d", "e"], ["a", "d", "f", "g"]]

The first element of each inner array represents a "father" and the second one a "son", so the final goal is to find grandsons, grandgrandsons... and then group them in bigger arrays, where all descendants are together.

Maybe it's an easy task, but I've been struggling to figure out how to get it!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you like to get all relations, without parts which are included in other results, you could get an object of all descendant and start with the parents who are not having parents.

let
    getNodes = k => parents[k]
        ? parents[k].flatMap(getNodes).map(a => [k, ...a])
        : [[k]],
    data = [["a", "b"], ["a", "c"], ["a", "d"], ["d", "e"], ["d", "f"], ["f", "g"]],
    parents = {},
    children = new Set,
    result;


for (const [p, c] of data) {
    (parents[p] ??= []).push(c);
    children.add(c);
}

result = Object
    .keys(parents)
    .filter(p => !children.has(p))
    .flatMap(getNodes);

result.map(a => console.log(...a));

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