巧用es6数组方法,求两数组的差集!

622 阅读1分钟

从arr1中。根据arr2中所有的项,排除过滤掉arr2中的项。

let arr1 = [

    {
        id:1,
        num:50,
        text:'1111111111'
    },
    {        id:2,
        num:100,
        text:'222222222'
    },
    {
        id:3,
        num:200,
        text:'33333333'
    },
    {
        id:4,
        num:300,
        text:'33333333'
    }];
let arr2 = [
    {
        id:1,
        num:50,
        text:'1111111111'
    },
    {   id:2,
        num:200,
        text:'222222222'
    }
]

let newArr = arr1 .filter(item=>!arr2 .some(ele=>ele.num===item.num));
//结果是:
//  [
//   {        
//         id:2,
//        num:100,
//        text:'222222222'
//    },
//    {
//        id:3,
//        num:200,
//        text:'33333333'
//   },
//   {
//      id:4,
//      num:300,
//      text:'33333333'
//   }
// ]