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

211
Views
get all keys of nested object in js

i have very simple question assume bellow object

const x={a:{b:"b"},c:{b:"b"}}

1)I want a method to return all of keys in my nested object eg ["a","b","c"] 2)I want a method to return all paths of my specific key eg for b=>["a","c"] using lodash we have method _.get(obj,path) that return object value by the path we pass to get method but what about times we haven't path or path is vague bellow code solve problem somehow

const findPath = (ob, key) => {
  const path = [];
  const keyExists = (obj) => {
    if (!obj || (typeof obj !== "object" && !Array.isArray(obj))) {
      return false;
    }
    else if (obj.hasOwnProperty(key)) {
      return true;
    }
    else if (Array.isArray(obj)) {
      let parentKey = path.length ? path.pop() : "";

      for (let i = 0; i < obj.length; i++) {
        path.push(`${parentKey}[${i}]`);
        const result = keyExists(obj[i], key);
        if (result) {
          return result;
        }
        path.pop();
      }
    }
    else {
      for (const k in obj) {
        path.push(k);
        const result = keyExists(obj[k], key);
        if (result) {
          return result;
        }
        path.pop();
      }
    }
    return false;
  };

  keyExists(ob);

  return path.join(".");
}

problems with above code are:1)is not clean 2)don't return all paths for example in x={a:{b:"b"},c:{b:"b"}} return just "a"

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could take a recursive approach and check if the key exists, then return this key, wrapped in a nested array or return the result of nested objects and map this with the actual key or return an empty array, which is no entry for a flat map.

const
    findPathes = (object, key) => key in object 
        ? [[key]]
        : Object.entries(object).flatMap(([k, v]) => {
            if (!v || typeof v !== 'object') return [];
            const pathes = findPathes(v, key);
            return pathes.length
                ? pathes.map(a => [k, ...a])
                : [];
        }),
    data = { a: { b: "b" }, c: { b: "b" }, d: { e: { f: { g: "g" }, h: { b: 'b' } } } } ;
    

console.log(findPathes(data, 'b'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!