Difference and intersection of two arrays containing objects

后端 未结 7 895
予麋鹿
予麋鹿 2020-11-28 06:10

I have two arrays list1 and list2 which have objects with some properties; userId is the Id or unique property:

list1          


        
7条回答
  •  情书的邮戳
    2020-11-28 06:52

    Use lodash's _.isEqual method. Specifically:

    list1.reduce(function(prev, curr){
      !list2.some(function(obj){
        return _.isEqual(obj, curr)
      }) ? prev.push(curr): false;
      return prev
    }, []);
    

    Above gives you the equivalent of A given !B (in SQL terms, A LEFT OUTER JOIN B). You can move the code around the code to get what you want!

提交回复
热议问题