The ES6 copy Array object has the following methods:
method One: object.assign () // object Shallow Copy, obj1 Copy to Obj2, this method to set Obj2 {}, not const OBJ2 = ""; Const OBJ1 = {a:1= {};object.assign (Obj2, Obj1) method two: ES6 extension operator (...) // object Shallow Copy, obj1 copy to obj2const OBJ1 = {a:1= {... obj1}; method Three: Deep copy // object deep Copy, obj1 copy to obj2< /c10>const obj1 = {a:1= Json.parse (json.stringify (obj1));
So-called deep copy and shallow copy:
Const OBJ1 = {a:1= = 2console.log (obj1) //{a:2}
A direct assignment like this is in fact a obj2 reference obj1, which points to the same storage address, regardless of which of the two will be affected.
For simple array objects (which do not contain reference data types), you can use a shallow copy method to eliminate this association effect.
For complex array objects (with reference data types, such as: {A:1,b:[1,2,3],c:{d:3}}} multiple nested types), a deep copy method is required.
In a real project, this is usually the case:
This true}). Then (res) = {if (res.data) {
Console.log (res.data) This . evaluationinfo = Res.data else { ... }})
Console.log print out will find that the RES.DATA data structure has changed some of the object's get and set functions, such a direct assignment is a reference, although not the data produced a bug, but if other places also need to use to Res.data, like this:
This true}). Then (res) = { if (res.data) { console.log (res.data) this. Evaluationinfo = Res.data this. selectevaluationinfo = res.data Else { ... }})
In this case, you need to use the object copy method to differentiate, you can use the three-point operator is simpler:
This true}). Then (res) = { if (res.data) { console.log (res.data) this. Evaluationinfo = {... res.data}this. selectevaluationinfo = {... res.data} Else { ... }})
Copying an object is just one use of the three-point operator, and it has several other uses, often used in array serialization:
function f (x, Y, z) { // ... }var args = [0, 1, 2];f (.... args)
ES6 extension Operator (three-point operator) "..." Usage and object copy