码迷,mamicode.com
首页 > 编程语言 > 详细

对象、数组 深度复制,支持对象嵌套数组、数组嵌套对象

时间:2018-08-06 15:49:36      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:else   for   对象   cti   war   const   cto   foreach   turn   

对象复制

Object.prototype.maps = function(){
  let newObj = new Object();
  let that = this;
  Object.keys(that).forEach(function(k){
    if( that[k].constructor == Object){
      newObj[k] = that[k].maps()
    }else if(that[k].constructor == Array){
      newObj[k] = that[k].maps()
    }else{
      newObj[k] = that[k]
    }
  })  
  return newObj;
}

数组复制

Array.prototype.maps = function(){
  let newArr = [];
  for(v of that){
    if( v.constructor == Object ){
      newArr.push(v.maps())
    }else if( v.constructor == Array ){
      newArr.push(v.maps())
    }else{
      newArr.push(v)
    }
  }
  return newArr;
}

个人原创,如有意见欢迎指正

对象、数组 深度复制,支持对象嵌套数组、数组嵌套对象

标签:else   for   对象   cti   war   const   cto   foreach   turn   

原文地址:https://www.cnblogs.com/YCxiaoyang/p/9430486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!