0

I have an array of array of objects (from a class) which is an attribute of my component and a function that takes it as an argument.

myFunc(MyClass[][]) {...}

In another function I modify this array of arrays. But sometimes I want to reverse the changes. I tried to to do a copy of it like that :

let temp = myArray.map(line => Object.assign([], line.map(obj => Object.assign({}, obj))));

When I log in the console it is identical. Then I replace the attribute with the copy if I need to reverse the changes. But when myFunc is called I got this :

ERROR TypeError: "myFunc is not a function"

The function works prefectly fine when I don't use a copy. Why is it happening ?

bm13563
  • 688
  • 5
  • 18

1 Answers1

0

You have only copied the reference. You need to copy the array to a new reference.

const copyOfArray = [...originalObject];

See also: How do I correctly clone a JavaScript object?

Severin Klug
  • 733
  • 5
  • 9