Reputation: 1863
I use TypeScript and I created a map using new Map<KEY, TYPE>
where KEY
is either a string, float or number.
In one special case I would like to use a custom class as a key to preserve some data structures:
class Foo {
name: string;
data1: any;
data2: any;
};
const map = new Map<Foo, ...>();
map.set(fooObject1, someData);
map.set(fooObject2, someData);
map.set(fooObject3, someData);
map.get('nameOfFooObject');
I would like to use name
as the key identifier, but as said I would need to preserve data1
and data2
as well. Is there a way to set a primitive type as a key with a function to point to the key?
Upvotes: 0
Views: 932
Reputation: 12837
Instead of having that custom object for key (which if you think about how map works you'll see is a bad idea), create a custom object for the value and add all properties that you want to preserve there.
Upvotes: 1