I'm trying to use the new (ES6) Map
objects in order to represent a map between properties and a value.
I have objects in a form similar to:
{key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N}
I want to group them based on both their key1 and key2 value.
For example, I want to be able to group the following by x
and y
:
[{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}]
And obtain a Map containing:
{x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4}
{x:3,y:4} ==> {x:3,y:4,z:4},{x:3,y:4,z:7}
{x:3,y:1} ==> {x:3,y:1,z:1}
In Python, I'd use tuples as dictionary keys. ES6 map allow arbitrary objects as keys but use the standard equality algorithm (===
) so objects are only equal by reference from what I can tell.
How can I accomplish this sort of grouping using ES6 maps? Alternatively, a solution using normal JS objects if there is an elegant way I overlooked.
I'd rather not use an external collections library - but if there is a better solution using one I'm interested in learning about it too.
See Question&Answers more detail:
os