I encountered a situation where I have an object structured 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'}
]
The expected output should be:
[
{attributeGroupId:2, attributeId: 11, name: 'Diamond'},
{attributeGroupId:2, attributeId: 12, name: 'Square'}
]
,
[
{attributeGroupId:1, attributeId: 9, name: '916'},
{attributeGroupId:1, attributeId: 1, name: '24K'}
]
This allows me to perform the cartesian product as demonstrated below:
[
{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, the goal is to maintain this functionality in a versatile manner since the number of attributeGroupId values can vary during runtime.
I propose that dividing the array into smaller arrays based on attributeGroupId would serve as the initial step.