zoukankan      html  css  js  c++  java
  • 对象、数组 深度复制,支持对象嵌套数组、数组嵌套对象

    对象复制

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

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

    你好!如果你有什么更好的建议或意见,请在评论区留言。感谢你的阅读!
  • 相关阅读:
    linux
    算法
    算法
    数据结构 与 算法
    mysql
    mysql
    mysql
    mysql
    【解决】Could not get JDBC Connection、java.lang.InterruptedException问题和排查过程
    git: unable to checkout working tree error: unable to create file Filename too long on windows
  • 原文地址:https://www.cnblogs.com/YCxiaoyang/p/9430486.html
Copyright © 2011-2022 走看看