Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!