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

201
Visualizações
Is there any way to get a parentNode from an Object?

Suppose I access a certain object that is inside an object, but I don't know what the key of the previous object is, is there any way to get the key of the parent of this object?

Basically I would like to get the relative node of a property of an object

var house = {
  bedroom: {
    bed: 'I am a bed'
  }
}

I got I'm a bed, I know her key is bed but I don't know she is in bedroom is there any way to get bedroom? just by knowing bed. It is similar to parentNode, but like in objects.

This is my wish: "bed.parentNoderesult bedroom" but in object;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One way you could do this is while you are iterating through the object, store the key as a variable.

function recurseObject(obj, parentNode) {
    let result;
    if (typeOf obj !== 'object') {
        return
    }
    for(const key in obj) {
        if(//house[key] is the element you want the parent node of) {
             return parentNode
        }
        result = recurseObj(obj[key], key)
    }
    return result
}

This function simply recurses through an object passing it the previous key. When you find the value you want the parent node for you simple return it. If the logic in the if block does not find what you want it will return undefined

  • This is simply a function to iterate through the object storing the previous key. I think there would be a much cleaner approach to this, however I would need to see more of your logic
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