Im looking to flatten the following structure below
[{
"intGroup": "France",
"events": [{
"title": "test admin event"
}]
}, {
"intGroup": "Hungary",
"events": [{
"title": "first instance of a title related to hungary"
}]
}, {
"intGroup": "Hungary",
"events": [{
"title": "another test item"
}]
}, {
"intGroup": "Hungary",
"events": [{
"title": "third instance of test item"
}]
}, {
"intGroup": "France",
"events": [{
"title": "second french item"
}]
}]
So that I can aggregate by country and then store the sub events into 1 child array as follows
[{
"intGroup": "France",
"events": [{
"title": "test admin event"
}, {
"title": "second french item"
}]
}, {
"intGroup": "Hungary",
"events": [{
"title": "first instance of a title related to hungary"
}, {
"title": "another test item"
}, {
"title": "third instance of test item"
}]
}]
Ideally id like to do this in the most performant way as possible, I was using a very crude method of checking, pushing pulling into arrays but I killed my local Javascript garbage collection system and the result set is not even that large !!
for (let index = 0; index < groups.length; index++) {
const element = groups[index];
let existsIndex = eventsTmp.findIndex( (item: any) => item.intGroup === element.title && item.intGroup !== undefined )
if( Number(existsIndex) >= 0 ) { // a record exists with this interest group, append to it
index = existsIndex
eventsTmp[index].events.push(<IEvent>prepareEvt)
}
else { // create fresh record
eventsIdsTmp.push({ eventID: event_id, intGrp: element.title })
eventsTmp.push({ intGroup: element.title, events: [prepareEvt] })
}
}