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

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

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