美文网首页
map()实现json数组的深复制

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

作者: peteg | 来源:发表于2017-01-06 14:35 被阅读0次

实现深复制代码片段:

<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转化成字符串类型;

相关文章

网友评论

      本文标题:map()实现json数组的深复制

      本文链接:https://www.haomeiwen.com/subject/ipatbttx.html