When one object changes the array's elements, why the other objects in the elements also change the

When using multi-layer cycle time, there will be an occasion when you want to change one element in the array of objects, and other objects in the page along with elements changed. According to the conditions already targeted to a particular object, but after changing discovery, the other or are making a difference.

The reason is this:

Array is a reference to an object, an array of variables stored on the stack, the element data is stored on the heap, the array assignment different objects, so the assignment object point to heap the same data, changing one element in the array object, other objects in the elements will change

 

Solution:

var arr = JSON.parse(JSON.stringify(array));

Guess you like

Origin www.cnblogs.com/zy-df/p/11823043.html