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

216
Vistas
Get path property from multi-dimentional array

Struggling to figure this out. What is the cleanest code to extract all paths from a multi-dimentional array?

Below is my data.

const Pages = [
  {
    path: ['home'],
  },
  {
    path: [''],
    subpages: [
      {
        path: ['about', 'congleton'],
        subpages: [
          {
            path: [ 'about', 'congleton', 'opening' ]
          }
        ]
      }
    ]
  }
]

And this is the expected outcome:

[ ['home'], [''], ['about', 'congleton'], [ 'about', 'congleton', 'opening' ] ]

This is what I have right now, however i'm getting 'home', '' rather than ['home'],[''].

I need to retain these first 2 arrays.

function Path(page) {
  let paths = page.path;
  if (page.subpages) {
    paths = [paths, (page.subpages).reduce((curr, subpage) => [...Path(subpage)], [])];
  }
  return paths;
}

const paths = Pages.reduce((curr, page) => curr.concat(Path(page)), []).flat();

console.log(paths);
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