Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda