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

202
Vistas
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 Respuestas
Responde la pregunta

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 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