es6深层次数组深拷贝

let arr = [
      {
        label: '1',
        children: [1, 2]
      }
    ]
let a = [{...arr[0]}]
a[0].children = []
console.log(arr[0].children)// 结果是[1,2]

let arr = [
      {
        label: '1',
        children: [1, 2]
      }
    ]
let a = [...arr[0]]
a[0].children = []
console.log(arr[0].children)// 结果是[]


//Deep Clone
obj1={a:0,b:{c:0}};
let obj3=JSON.parse(JSON.stringify(obj1));
obj1.a=4;
obj1.b.c=4;
console.log(JSON.stringify(obj3)); //{a:0,b:{c:0}}
posted @ 2018-12-29 15:57  Hello,Code  阅读(5078)  评论(0编辑  收藏  举报