I have a few objects that are all disparate, but which all have an id
field. I want to create a Set() to hold these, but am not sure how to specify this field to the Set constructor/prototype/etc. Is this doable?
Let's say I have two types of objects (I'll give typescript definitions for simplicity's sake):
interface Thing1 {
id: string;
someData: string[]
}
interface Thing2 {
id: string;
someString: string;
someOtherString: string
}
Now, I have two Arrays of these things, let's call them
array1: Thing1[];
array2: Thing2[];
What I'd like to do is create a new Set(array1)
and then add
each Thing2
in array2
, performing a conditional merge of the two arrays.
The unique key in each of these is the id
field - I was hoping there was some overloaded constructor or similar pattern that could be given a lambda (a la new Set(array1, (item) => item.id)
, maybe) which would specify the key to hash on for identity.