vue.js通过桶排序或ES6 Set实现数据集合计算(区分两组数据的新增和删除部分)

2 篇文章 0 订阅
 <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
<div id="app">
      <table>
            <tbody>
                <tr>
                    <td>oldList:{{oldList}}</td>
                </tr>
                <tr>
                    <td>newList:{{newList}}</td>
                </tr>
                <tr>
                    <td>表示newList与并集的差集(删掉的数据){{oldDifference}}</td>
                </tr>
                <tr>
                    <td>表示oldList与newList的交集(未修改的数据){{intersection}}</td>
                </tr>
                <tr>
                    <td>表示oldList与并集的差集(新增的数据){{newDifference}}</td>
                </tr>
            </tbody>
        </table>
    </div>

普通写法:

  new Vue({
        el: '#app',
        data: {
            oldList: [422, 423, 424, 425, 426, 427],
            newList: [426, 427, 428, 429, 430, 431],
            bucket: [], //定义一个空桶
            intersection: [],//old与new的交集
            oldDifference: [],//old与并集的差集
            newDifference: []//new与并集的差集
        },
        created() {
            this.oldList.forEach((item) => {  //设置桶的索引为new数据的数据,参数设置为0   
                this.bucket[item] = 0    //422: 0,423: 0,424: 0,425: 0,426: 0,427: 0
            })
            console.log(this.bucket);
            this.newList.forEach((item) => {   
                if (this.bucket[item] !== undefined) {
                    this.bucket[item] = 1
                } else {
                    this.bucket[item] = 2
                }
                // 422: 0,423: 0,424: 0,425: 0,426: 1,427: 1,428: 2,429: 2,430: 2,431: 2
            })
            var object = this.bucket
            for (const key in object) {
                if (object[key] == 0) {
                    this.oldDifference.push(key);
                } else if (object[key] == 2) {
                    this.newDifference.push(key);
                } else {
                    this.intersection.push(key);
                }
            }

        }
    })

ES6 Set写法:

  new Vue({
        el: '#app',
        data: {
            oldList: [422, 423, 424, 425, 426, 427],
            newList: [426, 427, 428, 429, 430, 431],
            intersection: [],//old与new的交集
            oldDifference: [],//old与并集的差集
            newDifference: []//new与并集的差集
        },
        created() {
            let union = new Set([...this.oldList, ...this.newList]);// 并集
            let oldSet = new Set(this.oldList)
            let newSet = new Set(this.newList)
            // newList与并集的差集
            this.oldDifference = [...new Set([...union].filter(x => !newSet.has(x)))];
            // newList与oldSet的交集
            this.intersection = [...new Set([...oldSet].filter(x => newSet.has(x)))];
            // oldList与并集的差集
            this.newDifference = [...new Set([...union].filter(x => !oldSet.has(x)))];
        }
    })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值