Hi I am struggling trying to use array.reduce() to get the grouping correct for the following scenario:
here is my starting array:
{
{
descriptionFunction: "Change",
processDate: "2019-12-12",
transactionEffectiveDate: "2019-12-12",
policyEffectiveDate: "2019-09-09",
policyExpirationDate: "2020-09-09",
documentID: "1234"
},
{
descriptionFunction: "Change",
processDate: "2019-12-06",
transactionEffectiveDate: "2019-12-07",
policyEffectiveDate: "2019-09-09",
policyExpirationDate: "2020-09-09",
documentID: "1235"
},
{
descriptionFunction: "Change",
processDate: "2019-11-29",
transactionEffectiveDate: "2019-11-29",
policyEffectiveDate: "2018-09-09",
policyExpirationDate: "2019-09-09",
documentID: "1236"
},
{
descriptionFunction: "Change",
processDate: "2019-11-29",
transactionEffectiveDate: "2019-11-29",
policyEffectiveDate: "2018-09-09",
policyExpirationDate: "2019-09-09",
documentID: "1237"
}
}
here is what i need to come up with:
{
"policyEffectiveDate": "2019-09-09",
"policyExpirationDate": "2020-09-09",
"transactions": [
{
"transactionEffectiveDate": "2019-12-12",
"descriptionFunction": "Change",
"documents": [
{
"descriptionSubType": "Policy",
"documentID": "1234"
}
]
},
{
"transactionEffectiveDate": "2019-12-07",
"descriptionFunction": "Change",
"documents": [
{
"descriptionSubType": "Policy",
"documentID": "1235"
}
]
}
]
},
{
"policyEffectiveDate": "2018-09-09",
"policyExpirationDate": "2019-09-09",
"transactions": [
{
"transactionEffectiveDate": "2018-11-29",
"descriptionFunction": "Change",
"documents": [
{
"descriptionSubType": "Policy",
"documentID": "1236"
},
{
"descriptionSubType": "Policy",
"documentID": "1237"
}
]
}
]
}
So there are two sets of groups in the output, the first by policyEffectiveDate and policyExpirationDate and underneath that by transactionEffectiveDate. I'm struggling trying to figure out how to implement complex groupings with array.reduce structure.
reduce? While it may be possible, it's probably a more confusing way to write it. Use ordinary loops orforEach()with side effects on a result variable.descriptionSubTypefrom?