This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10085|Reply: 2

[JavaScript] js array clone clone clone replicate data

[Copy link]
Posted on 2020-5-18 11:39:28 | | | |
There is an array of A, and I want to clone this array to get B, and I am modifying the value of array A, and I don't want to affect array B.

The code is as follows:

When using slice, Object.assign to copy,Modifying the value of the test1 array will affect other arrays.As shown below:



How can you modify the original array without affecting other cloned arrays?

Solution 1



Solution 2






Previous:Linux update yum update command
Next:JS amounts are separated by thousand commas
Posted on 2020-5-22 10:16:58 |
I generally implement it through JSON.parse(JSON.stringify(arr)), how does the efficiency of the two methods mentioned by the owner compare?
 Landlord| Posted on 2020-5-22 11:12:51 |
Continued for anticipation Posted on 2020-5-22 10:16
I generally implement it through JSON.parse(JSON.stringify(arr)), how does the efficiency of the two methods mentioned by the owner compare?

Without comparison, I don't feel like it makes much sense.

You can compare the performance and send it out
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com