Why change the value of an element of JS array, the other array element values change along with the

the reason:

  Reference type is an array, the array variable stored in the stack, the stack data stored in the element, the array assignment of different objects, the objects assigned the same data point to the heap, so changes to the elements in an array, the array elements are also other It will change.

solution:

  The principle is first converted to an array into a string objects, which will allocate a new block of memory, the data does not point to the same heap.

   例子1: var tmp = JSON.parse(JSON.stringify(array));

Guess you like

Origin www.cnblogs.com/KevinTong/p/11414770.html