js 数组深拷贝

本文参考自: https://www.cnblogs.com/lvonve/p/11334628.html

 

this.list = [
  {a: 1},
  {b: [1,2]}
];
this.newList = [];
this.newList = this.getList(this.list);

 

getList(arr) {
      let newArr = arr.constructor === Array ? [] : {};
      for (let i in arr) {
        if (arr[i].constructor === Object || arr[i].constructor === Array) {
          newArr[i] = this.getList(arr[i])
        } else {
          newArr[i] = arr[i]
        }
      };
      return newArr;
    }

 

posted @ 2020-07-30 16:38  本溢  阅读(621)  评论(0)    收藏  举报