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

179
Vistas
How to get parent object in multi-level object array javascript

I have a multidimensional array and each object has the same keys.

export const MENUS = [
    {
        "type": "main",
        "submenu": [],
        "path": "/a"
    },
    {
        "type": "main",
        "submenu": [
            {
                "type": "submenu",
                "submenu": [
                    {
                        "type": "submenu",
                        "submenu": [],
                        "path": "/b/4"
                    },
                ],
                "path": null
            },
            {
                "type": "submenu",
                "submenu": [],
                "path": "/b/1"
            }
        ],
        "path": null
    },
    {
        "type": "main",
        "submenu": [],
        "path": "/c"
    }
]

Now, I have a key and value (path: '/b/1') and I want to fetch parent object by key/value in array.

this is the result what I am looking for when I use { path: '/b/1' }.

{
        "type": "main",
        "submenu": [
            {
                "type": "submenu",
                "submenu": [
                    {
                        "type": "submenu",
                        "submenu": [],
                        "path": "/b/4"
                    },
                ],
                "path": null
            },
            {
                "type": "submenu",
                "submenu": [],
                "path": "/b/1"
            }
        ],
        "path": null
    },

If I use {path: '/c'}, then the result will be a root array. (equal to MENU) If anyone has a good solution, please advise me. Thanks.

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

0

function getObj(array, key, value) {
  return array.find(item => isThisItem(item))
  function isThisItem(current) {
    const entries = Object.entries(current)
    for (const [k, v] of entries) {
      if (k === key && v === value) return true
      if (Array.isArray(v)) {
        for (const f of v) {
          if (isThisItem(f)) return true
        }
      }
      if (typeof v === 'object' && v !== null) {
        if (isThisItem(v)) return true
      }
    }
  }
}

// usage
const obj = getObj(MENUS, 'path', '/b/1')
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