You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I have a pretty simple example to demonstrate my problem:
export interfaceShape{name: string;}consthashMap=newMap<[Shape,Shape],number>();constshape1={name:"A"};constshape2={name:"B"};hashMap.set([shape1,shape2],0);console.log(hashMap.has([shape1,shape2]));// returns false from some reason
So in this example, I have a Map that uses tuple of Shapes as a key.
And it returns false since when using [shape1, shape2] twice, it creates two different arrays in terms of address so the has method probably makes a shallow comparison between them, and as a result, returns false.
I thought maybe I could override has method to fits my needs? to shallow comparison Shape1 and Shape2 instead of the whole array.
Could it be done via collections package?
The text was updated successfully, but these errors were encountered:
Yes, dig for the details, but I’m pretty sure it checks if the object has a equals() method and allow what you describe, but the problem you’re going to have is that your tuple is just an array, and adding the equals method on each instance... right, not good. Best would be to have a type of yours instead of just a plain array, that way you have the right structure to consider them equal.
Hi,
So I have a pretty simple example to demonstrate my problem:
So in this example, I have a Map that uses tuple of Shapes as a key.
And it returns false since when using
[shape1, shape2]
twice, it creates two different arrays in terms of address so thehas
method probably makes a shallow comparison between them, and as a result, returns false.I thought maybe I could override
has
method to fits my needs? to shallow comparison Shape1 and Shape2 instead of the whole array.Could it be done via collections package?
The text was updated successfully, but these errors were encountered: