码迷,mamicode.com
首页 > 编程语言 > 详细

数组对象 级别 交集

时间:2019-08-13 20:21:35      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:pre   差集   func   ===   one   数据   ash   select   array   

有以下数据:
 let a = new Set([
    {
        ‘categoryId‘: 1,
        ‘categoryIdLevelOne‘: 750611334,
        ‘categoryIdLevelThree‘: 750611336,
        ‘categoryIdLevelTwo‘: 750611335,
        ‘id‘: 2697,
        ‘level‘: 3,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 1
    },
    {
        ‘categoryId‘: 2,
        ‘categoryIdLevelOne‘: 750611472,
        ‘categoryIdLevelTwo‘: 750611473,
        ‘id‘: 2701,
        ‘level‘: 2,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 2
    },
    {
        ‘categoryId‘: 3,
        ‘categoryIdLevelOne‘: 750611487,
        ‘categoryIdLevelTwo‘: 750611488,
        ‘id‘: 2702,
        ‘level‘: 2,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 1
    }
])
let b = new Set([
    {
        ‘categoryId‘: 2,
        ‘categoryIdLevelOne‘: 750611334,
        ‘categoryIdLevelThree‘: 750611336,
        ‘categoryIdLevelTwo‘: 750611335,
        ‘id‘: 2697,
        ‘level‘: 3,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 1
    },
    {
        ‘categoryId‘: 3,
        ‘categoryIdLevelOne‘: 750611472,
        ‘categoryIdLevelTwo‘: 750611473,
        ‘id‘: 2701,
        ‘level‘: 2,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 2
    },
    {
        ‘categoryId‘: 4,
        ‘categoryIdLevelOne‘: 750611487,
        ‘categoryIdLevelTwo‘: 750611488,
        ‘id‘: 2702,
        ‘level‘: 2,
        ‘shopId‘: 12430,
        ‘skipLayoutFlag‘: false,
        ‘status‘: 1
    }
])

交集
[...a].filter(x => [...b].some(y => y.categoryId === x.categoryId))

//或者

Array.from(a).filter(x => Array.from(b).some(y => y.categoryId === x.categoryId))

差集

[...a].filter(x => [...b].every(y => y.categoryId !== x.categoryId))

去重

 this.selectTable = this.selectTable.reduce(function(item, next) {
        hash[next.id] ? "" : (hash[next.id] = true && item.push(next));
        return item;
      }, []);

 

数组对象 级别 交集

标签:pre   差集   func   ===   one   数据   ash   select   array   

原文地址:https://www.cnblogs.com/nns4/p/11348148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!