Vue實現對數組、對象的深拷貝、複製

方法一javascript

const clone = (obj) => {
  var o;
  // 若是  他是對象object的話  , 由於null,object,array  也是'object';
  if (typeof obj === 'object') {
    
    // 若是  他是空的話
    if (obj === null) {
      o = null;
    }
    else {
  
      // 若是  他是數組arr的話
      if (obj instanceof Array) {
        o = [];
        for (var i = 0, len = obj.length; i < len; i++) {
          o.push(clone(obj[ i ]));
        }
      }
      // 若是  他是對象object的話
      else {
        o = {};
        for (var j in obj) {
          o[ j ] = clone(obj[ j ]);
        }
      }
      
    }
  }
  else {
    o = obj;
  }
  return o;
};

export default clone;

 

方法二java

computed: {  
     data: function () {  
         var obj={};  
         obj=JSON.parse(JSON.stringify(this.templateData)); //this.templateData是父組件傳遞的對象  
         return obj  
    }  
 }

參考地址數組

相關文章
相關標籤/搜索