JS求两个对象数组的差集

let arr1 = [ 
    {fpcid: "2da37fdfe66140089dc3367b21cc6194", fpcNameCode: "34"},
    {fpcid: "2da37fdfe66140089dc3367b21cc6195", fpcNameCode: "33"},
    {fpcid: "2da37fdfe66140089dc3367b21cc6196", fpcNameCode: "35"},
    {fpcid: "2da37fdfe66140089dc3367b21cc6197", fpcNameCode: "36"}
];
let arr2 = [ 
    {fpcid: "2da37fdfe66140089dc3367b21cc6194", fpcNameCode: "34"},
    {fpcid: "2da37fdfe66140089dc3367b21cc6195", fpcNameCode: "33"}
];



JS求两个对象数组的差集

[
        {fpcid: "2da37fdfe66140089dc3367b21cc6196", fpcNameCode: "35"},
        {fpcid: "2da37fdfe66140089dc3367b21cc6197", fpcNameCode: "36"}
]
阅读 5.1k
评论
    5 个回答
    • 2.8k

    这个可以将数组项转成字符串来处理,以下为代码:

      
      console.log(((a = arr1.map(JSON.stringify),b = arr2.map(JSON.stringify))=>{ return a.concat(b).filter(c => !a.includes(c) || !b.includes(c)).map(JSON.parse)})())
      //可拆分成,arr1和arr2为你提供的数据数组
      function diffrence(a,b){
          a = a.map(JSON.stringify);
          b = b.map(JSON.stringify);
          return a.concat(b).filter(v => !a.includes(v) || !b.includes(v)).map(JSON.parse)
      }
      diffrence(arr1,arr2);
      
      相似问题
      推荐文章