[Solution] Loop insert objects into the array. When the value of the previous object changes, the objects in the array also change.

Problems encountered:

Loop an array, insert the required attributes into an object [newObj], and then push to an array [newArr]. When the value of the object changes each time, the value of the object that has been inserted into the array will also change accordingly;

 newArr.push(newObj)

Solution:

Every time you push to the array [newArr], make a deep copy of the object [newObj];
each time the array is pushed, the same object address is referenced, so when the object changes every time, the objects in the array will also Change accordingly;
using deep copy is equivalent to creating a new memory address of a new object every time

newArr.push(JSON.parse(JSON.stringify(newObj)))

Guess you like

Origin blog.csdn.net/JunVei/article/details/128562739