Group the Array item by one of its property, Problem is, that property is array.
Rules: Every Items in that property must be same to make it in single group.
Sample Array:
[{name:'classOne',id:"c1",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]},
{name:'classTwo',id:"c2",student:[{name:'student1',id:"student1"}]},
{name:'classThree',id:"c3",student:[{name:'student3',id:"student3"}]},
{name:'classFour',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]},
{name:'classFive',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"},{name:'student3',id:"student3"}]}
]
Expected Output:
{
key1:[
{name:'classOne',id:"c1",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]
{name:'classFour',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]
],
key2:[
{name:'classTwo',id:"c2",student:[{name:'student1',id:"student1"}]},
],
key3:[
{name:'classThree',id:"c3",student:[{name:'student3',id:"student3"}]},
],
key4:[
{name:'classFive',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"},{name:'student3',id:"student3"}]}
]
}
My sample Function
export function groupByArrayKey<T>(key: string, arrayValue: T[]) {
let result = {};
return arrayValue.reduce((result, currentVal) => {
const currentArray = getDeepValue(currentVal, key);
const resultKey = currentArray[0].id;
const keyWithLen = resultKey + currentArray.length;
if (resultKey in result) {
const resultArray = getDeepValue(result[resultKey][0], key);
const isEql = isEqual(resultArray, currentArray);
if (isEql) {
(result[resultKey] = result[resultKey] || []).push(currentVal);
} else {
(result[keyWithLen] = result[keyWithLen] || []).push(currentVal);
}
} else if (keyWithLen in result) {
const resultArray = getDeepValue(result[keyWithLen][0], key);
const isEql = isEqual(resultArray, currentArray);
if (isEql) {
delete result[keyWithLen];
(result[resultKey] = result[resultKey] || []).push(resultArray, currentVal);
} else {
const dummyKey = Math.random();
(result[dummyKey] = result[dummyKey] || []).push(currentVal);
}
} else {
console.log('third');
(result[resultKey] = result[resultKey] || []).push(currentVal);
}
return result;
}, {});
}
expect final output can be "array of array" / "object of array", anything is fine.
GroupBy: Student
The below function is working, But, is there any other to write it, in compact way?