Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Find all the key-value pair and returning the path where the pair is in deep nested objects in JS

I am trying to create a function which must do the following:

  • takes a JavaScript object as first parameter

  • the object can have any structure and depth

  • takes a string containing a key as second parameter

  • returns a map with all values that are found for that key within the object o the key in the resulting map shall be the path to the key within the object where "/" is used as separator o the root key for the map is the name of the object

the result has to be something like:

let obj = {
    id: 1,
    b: "value",
    c: 404,
    d: { 
        id: "value", 
        b: { 
            id: 1, 
            b: 2 
        } 
    },
};

myFunction(obj, 'id')
// [ ["" -> 1] ["d" -> "value"] ["d/b" -> 1] ]

I am doing the following:

let **findAllByKey** = (obj, keyToFind) =>
  Object.entries(obj).reduce(
    (acc, [key, value]) =>
      key === keyToFind
        ? acc.concat(value)
        : typeof value === "object"
        ? acc.concat(findAllByKey(value, keyToFind))
        : acc,
    []
  );


console.log(findAllByKey(obj, "id"));

But this only returns the values, and I am having trouble to get the path: "/d/b", and it says It must be done with a map constructor ( New Map() ), but with map, I have no clue how to do it.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could get the entries and iterate by checking the values.

const
    getPathes = (object, key) => Object
        .entries(object)
        .reduce((r, [k, v]) => {
            if (!v || typeof v !== 'object') return r;
            r.push(...getPathes(v, key).map(([l, v]) => [k + (l && '/') + l, v]));
            return r;
        }, key in object ? [['', object[key]]] : []),
    object = { id: 1, b: "value", c: 404, d: { id: "value", b: { id: 1, b: 2 } } };

console.log(getPathes(object, 'id')); // [["", 1], ["d", "value"], ["d/b" , 1]]

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use jsonpath library.

Sample:

const jp = require('jsonpath');
const result = jp.nodes(obj, '$..id');

Result:

[
  { path: [ '$', 'id' ], value: 1 },
  { path: [ '$', 'd', 'id' ], value: 'value' },
  { path: [ '$', 'd', 'b', 'id' ], value: 1 }
]

After that you get objects with path and value properties and can transform result as you want.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda