Problems copy depth javascript array / arrays of objects

I. Description of the problem

  A registration page in the project need to check two pieces of information (information and a message b), because the information I and two information data owned by the same, so the background only to return an array of objects in the foreground and then set up two a List array to receive and distinction. Prototype below;

  

 

 

   Test question that arises is: Check any information or a message Second option, the corresponding information in the other option will be checked. For example, I checked the name and information of a mobile phone, the information II names and phone will be checked on. As shown below:

 

 

 Second, the error code

In fact, after the project had a problem before because the object in memory address points caused, soon we realized that this is because the issue is still arrays point and give some solutions, however, does not seem to work.

Errors and attempts to modify the code:

// The first error code returned directly to the requesting data were assigned to the array; 
the this .firstList = data.body.dataList;
 the this .secondList = data.body.dataList;
 // identified the problem, try to solve the problem several schemes 
the this .firstList = the this .firstList .concat (data.body.dataList);
 the this .secondList = the this .secondList .concat (data.body.dataList);
 // another method of 
the this .firstList = new new the Array ( data.body.dataList ...);
 the this .secondList = new new the Array (... data.body.dataList);
 // a further 
the this .firstList = [... data.body.dataList];
 the this.secondList = [...data.body.dataList];

The problem is still not resolved, found on the Internet are nothing more than the two I wrote it myself, then suddenly saw a copy of the article written about a two-dimensional array, for one-dimensional arrays we can use the above method to solve the problem of address points but for two-dimensional array, just copy the first dimension of the array, the first dimension because the array is stored in a reference to the second dimension, the second dimension is the actual storage of their content, so the above methods can not solve problem.

Then think, my solution is only copied the array, and the array of objects which did not copy, so it needs to go deep assignment object. This is a copy of the object in question array.

Resolution code:

 this.babyList = data.body.dataList.map(o => ({...o}));
 this.parentList = data.body.dataList.map(o => ({...o}));

Third, the solution related knowledge

(1), ES6 array extended operator

Extended operator is three dots (...), the array into a sequence of parameters separated by commas.

console.log(...[1, 2, 3])
// 1 2 3

 

To be continued .........

Guess you like

Origin www.cnblogs.com/songForU/p/11469498.html