I have a scenario where I have an object just like this.
[
{attributeGroupId:2, attributeId: 11, name: 'Diamond'}
{attributeGroupId:1, attributeId: 9, name: '916'}
{attributeGroupId:1, attributeId: 1, name: '24K'}
{attributeGroupId:2, attributeId: 12, name: 'Square'}
]
Expected result:
[
{attributeGroupId:2, attributeId: 11, name: 'Diamond'},
{attributeGroupId:2, attributeId: 12, name: 'Square'}
]
,
[
{attributeGroupId:1, attributeId: 9, name: '916'},
{attributeGroupId:1, attributeId: 1, name: '24K'}
]
So I can make the cartesian product of it just like this,
[
{attributeId: 11-9, name: 'Diamond-916'},
{attributeId: 11-1, name: 'Diamond-24K'},
{attributeId: 12-9, name: 'Square-916'},
{attributeId: 12-1, name: 'Square-24K'},
]
Now I want to keep this logic as generic as possible as the number of attributeGroupId is not known during runtime.
I think that splitting the array into multiple smaller array on the basis of attributeGroupId should be the first step.