1

I've following scenario where I'm merging two object using Object.assign function

Object.assign(
    {}, 
    {
        a:1, 
        b: {
            c:{
                d: 1, 
                e: 1
            }
        }
    }, 
    {
        a:2, 
        b: {
            c: {
                d:2
            }
        }
    }
);

and getting

{
    a: 2,
    b: {
        c: {
            d: 2
        }
    }
}

but I am expecting to get

{
    a: 2,
    b: {
        c: {
            d: 2,
            e: 1
        }
    }
}

I.e., e should not be eliminated. I want to retain properties in source1 hierarchy that are absent in source2 object hierarchy. Am I using Object.assign in wrong way?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ejaz
  • 8,719
  • 3
  • 34
  • 49
  • @JamesThorpe yes I stumbled upon that question and figured it was more than a year old. So is "No it doesn't" still the answer to merging objects? – Ejaz Mar 07 '17 at 10:46
  • Actually this answer (http://stackoverflow.com/a/34749873/2219831) to the same question worked for me – Ejaz Mar 07 '17 at 10:52
  • Yeah, the ES6 spec hasn't changed in that time, so it's still not built in. But yes, always look beyond the accepted answer - there's quite a few fairly upvoted answers on that question. – James Thorpe Mar 07 '17 at 10:54

0 Answers0