Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!