0

I try to do this with 2 objects in Javascript

const a = {
  a: 1,
  b: {
    c: 1,
    d: 2
  }
};

const b = {
  b: {
    c: 3
  }
};

Object.assign({}, a, b)

and want to see such result

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

but I got this

 a: 1,
 b: {
   c: 3
 }

Where is the mistake ? Is it posible to do or not ? How to "merge" 2 objects with deep structure

Waldi
  • 39,242
  • 6
  • 30
  • 78
nl pkr
  • 656
  • 1
  • 11
  • 21
  • 1
    You'd need to use a recursive merging utility; all assign does is assign, which will overwrite key values. – Dave Newton Aug 19 '16 at 15:04
  • [MDN has a warning for deep cloning](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Deep_Clone). You'll need to use another method. – zzzzBov Aug 19 '16 at 15:04

0 Answers0