Quantcast
Channel: Active questions tagged set-difference - Stack Overflow
Viewing all articles
Browse latest Browse all 35

Difference and intersection of two arrays containing objects

$
0
0

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

list1 = [    { userId: 1234, userName: 'XYZ'  },     { userId: 1235, userName: 'ABC'  },     { userId: 1236, userName: 'IJKL' },    { userId: 1237, userName: 'WXYZ' },     { userId: 1238, userName: 'LMNO' }]list2 = [    { userId: 1235, userName: 'ABC'  },      { userId: 1236, userName: 'IJKL' },    { userId: 1252, userName: 'AAAA' }]

I'm looking for an easy way to execute the following three operations:

  1. list1 operation list2 should return the intersection of elements:

    [    { userId: 1235, userName: 'ABC'  },    { userId: 1236, userName: 'IJKL' }]
  2. list1 operation list2 should return the list of all elements from list1 which don't occur in list2:

    [    { userId: 1234, userName: 'XYZ'  },    { userId: 1237, userName: 'WXYZ' },     { userId: 1238, userName: 'LMNO' }]
  3. list2 operation list1 should return the list of elements from list2 which don't occur in list1:

    [    { userId: 1252, userName: 'AAAA' }]

Viewing all articles
Browse latest Browse all 35

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>