es6深層次數組深拷貝

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

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}}
相關文章
相關標籤/搜索