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

137
Vistas
JavaScript : Get Path of a given key-value pair in a nested Object (or JSON)

Let's assume,I have a nested object, but it contains an unique key with an unique identifier as a value.

How do I get the path until I get the key-value pair by its path, e.g. by using lodash.

import _ from 'lodash';
    
_.get(object, path);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So, because nobody knew an answer, I found my own solution:

function getPath(obj, givenKey, givenValue) {
    for(var key in obj) {                                   
        if(obj[key] && typeof obj[key] === "object") {      
            var result = getPath(obj[key], givenValue, givenKey);              
            if(result) {                                    
                result.unshift(key);                        
                return result;                             
            }
        } else if(obj[key] === givenValue && key === givenKey ) {                      
            return [key];                                   
        }
    }
}
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