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

287
Visualizações
Javascript recursive function on nested object

We have the following kind of object:

const obj = {
    a: { "=": 0 },
    b: {
        c: { "=": 1 },
        d: {
            e: { "=": 2 },
            f: {
                g: { "=": 3 }
            }
        }    
    }

}

I am trying to write a function that returns an array with the object keys, including the nested ones, but groups them if they are nested and their values is not { "=": "something" }. To better explain I'll write a sample output desired for the object above:

["a", "b.c", "b.d.e", "b.d.f.g"]

All I have achieved for now is a function that only traverses the first level, but I admit I got stuck on the recursive part where the same function will apply on every level of the object until it reaches an entry of type { "=": "something" }. Follows my actual code:

function nestedObject(obj) {
    const operator = "="
    const attributes = []
    void (function traverse(obj) {
        if (isObject(obj)) { // <-- isObject(obj) just checks if typeof obj == "object", !Array.isArray(obj), obj !== null
            for (const [k,v] of Object.entries(obj)) {
                let attr = k
                for (const [k_,v_] of Object.entries(v)) {
                    if (operator !== k_) {
                        attr += "." + k_
                        // here is where the recursive part should start 
                        // but I didn't figure out yet how to make it right. 
                        // Of course calling traverse(v_) doesn't give the desired result.
                    }
                    attributes.push(attr)
                }
            }
        }
    })(obj)
    console.log(attributes) // <-- will output ["a", "b.c", "b.d"]
}

I hope I did explain well enough. I don't even know if this is possible, but I think it is. Would love to hear some ideas.

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

0

I keep handy some utility functions to make such coding easier. One of them takes an object and lists all the leaf paths in that object, as an array of keys. Building your function atop that becomes simply a matter of removing trailing = parts and joining the results with dots:

const getPaths = (obj) =>
  Object (obj) === obj
    ? Object .entries (obj) .flatMap (([k, v]) => getPaths (v) .map (p => [k, ...p]))
    : [[]]

const nestedObject = (obj) =>
  getPaths (obj) 
    .map (p => p .at (-1) == '=' ? p .slice (0, -1): p)
    .map (p => p .join ('.'))

const obj = {a: {"=": 0}, b: {c: {"=": 1}, d: {e: {"=": 2}, f: {g: {"=": 3}}}}}

console .log (nestedObject (obj))

This is a slightly less sophisticated version of getPaths than I usually write. Usually I distinguish between numeric array indices and string object keys, but that's not relevant here, so this version is simplified.

The main function should be clear enough on top of that.

Note that this does not prohibit internal = keys, so if, parallel to g in your input, you also had an h with an = key, like this:

            f: {
                g: { "=": 3 },
                h: { "=": {i: { "=": 4}}}
            }

we would yield this result:

["a", "b.c", "b.d.e", "b.d.f.g", "b.d.f.h.=.i"]

If you also wanted to remove those, you could add this between the two map calls:

    .filter (p => ! p.includes ('='))
about 4 years ago · Juan Pablo Isaza Relatório

0

Here's a small generator function that, given an object, recursively yields pairs like [ array-of-nested-keys, value ]:

function* enumRec(obj, keys=[]) {
    yield [keys, obj];
    if (obj && typeof obj === 'object')
        for (let [k, v] of Object.entries(obj))
            yield *enumRec(v, keys.concat(k))
}

To solve the problem at hand, iterate enumRec and collect keys that end with a =:

for (let [keys, _] of enumRec(obj)) {
    if (keys.at(-1) === '=')
        console.log(keys.slice(0, -1).join('.'))
}
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