1. 程式人生 > >es6深層次陣列深拷貝

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}}