We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现方式1 - for...in
实现方式1
let obj = { name: 'qy', sno: '123456', info: { address: 'this is beijing', } } let new_obj = {}; for(let item in obj) { new_obj[item] = obj[item]; } new_obj.info.address = 'zhejiang'; // { name: 'qy', sno: '123456', info: { address: 'zhejiang' } } console.log(new_obj); console.log(obj);
实现方式2 - es6中的...
实现方式2
let obj = { name: 'qy', sno: '123456', info: { address: 'this is beijing', } } let new_obj = {...obj}; new_obj.info.address = 'zhejiang'; // { name: 'qy', sno: '123456', info: { address: 'zhejiang' } } console.log(new_obj); console.log(obj);
实现方式3 - Object.assign
实现方式3
let obj = { name: 'qy', sno: '123456', info: { address: 'this is beijing', } } let new_obj = Object.assign({}, obj); new_obj.info.address = 'zhejiang'; // { name: 'qy', sno: '123456', info: { address: 'zhejiang' } } console.log(new_obj); console.log(obj);
实现方式1 - JSON方法
let obj = { name: 'qy', sno: '123456', info: { address: 'this is beijing', } } let new_obj = JSON.parse(JSON.stringify(obj)); new_obj.info.address = 'zhejiang'; console.log(new_obj); //{ name: 'qy', sno: '123456', info: { address: 'zhejiang' } } console.log(obj); // { name: 'qy', sno: '123456', info: { address: 'this is beijing' } }
实现方式2 - 递归
function deepCopy(new_obj, old_obj) { for(let k in old_obj) { let item = old_obj[k]; if(item instanceof Array) { //数组 new_obj[k] = []; deepCopy(new_obj[k], item); }else if(item instanceof Object) { //对象 new_obj[k] = {}; deepCopy(new_obj[k], item); }else { //普通数据 new_obj[k] = item; } } } let obj = { name: 'qy', sno: '123456', info: { address: 'this is beijing', } } new_obj = {}; deepCopy(new_obj, obj); new_obj.info.address = 'zhejiang' console.log(new_obj); //{ name: 'qy', sno: '123456', info: { address: 'zhejiang' } } console.log(obj); // { name: 'qy', sno: '123456', info: { address: 'this is beijing' } }
递归方式3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script> function deepCopy(source) { if(source instanceof Array) { let arr = []; for(let i=0;i<source.length; i++){ arr.push(deepCopy(source[i])); } return arr; }else if(source instanceof Object) { let obj = {} for(let k in source) { obj[k] = deepCopy(source[k]); } return obj; }else { return source; } } const list = { name: 'qy', list: [1,2,3,4,5], age: 18, school: { name: 'aaa', address: { add1: 'a', add2: 'b' } } } const obj = deepCopy(list); obj.school.address.add1 = 'ad'; obj.list = [2,4,5]; console.log(list); console.log(obj); </script> </head> <body> </body> </html>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
浅拷贝
实现方式1
- for...in实现方式2
- es6中的...实现方式3
- Object.assign深拷贝
实现方式1
- JSON方法实现方式2
- 递归递归方式3
The text was updated successfully, but these errors were encountered: