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

86
Visualizações
Changing and/or merging two JSON Objects structures

Lets say we are given a following JSON:

{
    "a": {
        "title": {
            "text": "text a"
        }
    },
    "b": {
        "title": {
            "text": "text b"
        }
    }
}

How could I convert and/or merge this into the following structure:

{
    "title": {
        "text": {
            "a": "text a",
            "b": "text b"
        }
    }
}

or a different example:

let aObject = {
    "level1": {
        "level2": {
            "level3": "text a"
        }
    }
}
let bObject = {
    "level1": {
        "level2": {
            "level3": "text b"
        }
    }
}
let cObject = {
    "level1": {
        "level2": {
            "level3": "text c"
        }
    }
}

into

{
    "level1": {
        "level2": {
            "level3": {
                "aObject": "text a",
                "bObject": "text b",
                "cObject": "text c"
            }
        }
    }
}

It might be very easy as I am missing a keyword describing this feature, or it might be complicated to archive this. Any help is appreciated.

Working example, looking for simpler solution:

let object = {
    "a": {
        "title": {
            "text": "text a"
        }
    },
    "b": {
        "title": {
            "text": "text b"
        }
    }
}

function reduce(node, object, result = {}) {
    for (let key in object) {
        if (typeof result[key] === 'undefined') {
            result[key] = {}
        }

        if (typeof object[key] === 'string') {
            result[key][node] = object[key]

        } else {
            reduce(node, object[key], result[key])
        }
    }

    return result
}

let result = Object.entries(object).reduce((result, [key, value]) => {
    return reduce(key, value, result)
}, {})

console.log(result)

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

0

Can do in this way.:)

let obj = {
  a: {
    title: {
      text: "text a"
    }
  },
  b: {
    title: {
      text: "text b"
    }
  }
};




let test = {};
test["title"] = {};
test.title["text"] = {};

Object.keys(obj).forEach((key) => {
  test.title.text[key] = `text ${key}`;
});

console.log(test);

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