Compare two arrays and find uniques by id.


Compare two arrays and find uniques by id.

I have two arrays, lets say A and B. I need to find unique items in array B by comparing with object ID. How can I do this? I can't use equal or something as the objects are little bit different I think. I am also using angular, so it creates extra hash values and therefore the objects probably are not comparable? Lodash contains didn't work, indexOf didn't work. What am I doing wrong 😛

const a = [ {id: 123, randomvalue: 'hello'} ]; const b = [ {id: 123, randomvalue: 'hello', othervalue: 'sup'}, {id: 125, randomvalue: 'sup', othervalue: 'hello'} ]; 

and the unique values array should be:

const uniques = [ {id: 125, randomvalue: 'sup', othervalue: 'hello'} ]; 

Submitted July 17, 2017 at 07:43PM by shnigi
via reddit http://ift.tt/2uAmLT2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s