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

144
Visualizações
I have an array that I need to transform in a object (JAVASCRIPT)

Well, basically I have an array that has an objects with names and this objects has an array of objects inside, looks likes this:

var array = [
  {articles: [
    {number: "123"},
    {number: "143"},
  ]},
  {paragraph: [
    {number: "197"},
  ]},
]

And I'm really willing to get a object value in return, like this

{articles: [...], paragraph: [...]}

Can someone help me, please?

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

0

Iterate over the array, and use Object.entries to get the keys and values.

const array=[{articles:[{number:"123"},{number:"143"}]},{paragraph:[{number:"197"}]}];

const out = {};

for (const obj of array) {
  const [[key, value]] = Object.entries(obj);
  out[key] = value;
}

console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could group by the outer properties of the nested objects.

const
    array = [{ articles: [{ number: "123" }, { number: "143" }] }, { paragraph: [{ number: "197" }] }],
    result = array.reduce((r, o) => {
        Object
            .entries(o)
            .forEach(([k, a]) => (r[k] ??= []).push(...a));

        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

This can be done using an array reducer.

const array = [
  { articles: [{ number: "123" }, { number: "143" }] },
  { paragraph: [{ number: "197" }] },
];

const formatted = array.reduce((accumulator, currentValue) => {
    const [[key, value]] = Object.entries(currentValue);
    return { ...accumulator, [key]: value }
}, {});

console.log(formatted);

What we're doing is initializing our reducer with an empty object on line 9, and iterating over the array. Each time we iterate, we're returning the object with the new key and value appended to the end of it.

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