2

I have the following code:

let a =  this.menu.getMenuItems().find((item) => item.$entityType === val);
let b = Object.assign({}, a);

this.dictChildren = b.children.map((item) => {
});

First I try to find element in array then create copy.

After I attempted to modify found element using map(), despite

 let b = Object.assign({}, a);

It modifies original array.

How map only copied object b?

POV
  • 11,293
  • 34
  • 107
  • 201
  • 3
    You need to **deep copy** the object. – briosheje Aug 09 '19 at 12:57
  • How to do that? – POV Aug 09 '19 at 13:09
  • NinaScholz conveniently linked the duplicate that explains how to do that in the most efficient way, you should check it: https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – briosheje Aug 09 '19 at 13:10

1 Answers1

1

Object.assign makes a shallow copy and not a deep copy.

vatz88
  • 2,422
  • 2
  • 14
  • 25