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

155
Visualizações
Merge nested object with array of objects with deepmerge

I am using library https://www.npmjs.com/package/deepmerge to deep merge two nested objects with array of objects but I am not getting expected result.

       const x = {
                foo: { bar: 1 },
                loo: {
                  array: [
                    {
                      too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }],
                    },
                  ],
                },
              };

              const y = {
                foo: { bar: 4 },
                loo: {
                  array: [
                    {
                      too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }],
                    },
                  ],
                },
              };

and console.log(deepmerge(x, y)); gives me this result:

        const result = {
                foo: { bar: 4 },
                loo: {
                  array: [
                    {
                      too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }],
                    },
                    {
                      too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }],
                    },
                  ],
                },
              };

but I would expect this:

              const expectedResult = {
                foo: { bar: 4 },
                loo: {
                  array: [
                    {
                      too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }],
                    },
                  ],
                },
              };

Am I missing something?

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

0

In this section of the doc, it mentions that

By default, arrays are merged by concatenating them.

So you have to provide another option to override this default behavior, this is mentioned below, by using arrayMerge option

const x = { foo: { bar: 1 }, loo: { array: [{ too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }], }, ], }, };
const y = { foo: { bar: 4 }, loo: { array: [{ too: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 5 }], }, ], }, };

console.log(
  deepmerge(
    x,
    y,
    { arrayMerge: (destinationArray, sourceArray, options) => sourceArray }
  )
);
<script src="https://unpkg.com/deepmerge@4.2.2/dist/umd.js"></script>

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