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

实现深复制代码片段:

<li v-for="(items, i) in shopFileListData" @click="onCheckBox(items)" :key="items.shopFileId" :class="{'li-checked': liChecked(items)}">
   <div class="source-file" style="width: 200px;">
        .....            
  </div>
</li>
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转化成字符串类型;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容