map()实现json数组的深复制

实现深复制代码片段:

  • .....
  • export default { data() { return { shopFileListData:[], fileList:[], } }, computed:{ allChecked:{ get: function() { return this.checkedCount == this.shopFileListData.length; }, set: function (value) { if(value){ this.checked = this.shopFileListData.map((items) => { this.fileList.push(items); return items.shopFileId+''; }) //this.fileList = this.shopFileListData; console.log(JSON.stringify(this.fileList)) }else { this.checked = []; this.fileList = [] } } }, checkedCount: { get: function() { return this.checked.length; } }, }, }

    this.shopFileListData为json数组,通过map()把里面的items,push到this.fileList中,这样进行了深复制。

    注:
    用ES6函数箭头,才使this.fileList的this指向全局vue对象;
    items.shopFileId+''是为了把items.shopFileId转化成字符串类型;

    你可能感兴趣的:(map()实现json数组的深复制)