I have an array object with nested objects inside. In each object of the array object is a nested object, i want to do the following:
-
Iterate through the testInfo property of the participantDetails property of the testObj arrayObject.
-
I want to create or get another object by using the JavaScript Object reduce function to group the Agency property of the participantDetails of participants property of the testObj array object.
I have written and edited my codes but couldn’t get this right. Below are my code:
const testObj = [{
id: 1,
testName: 'Contingent/20sw',
testTest: 125,
startDate: '19-10-2024',
endDate: '21-11-2024',
testCenter: 'Onne',
participants:{
participantDetails: {
id: 1,
agency: 'Farehnsic Enterprise',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 25,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 2,
agency: 'Platinum Logistics',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 3,
agency: 'Chevron PLC',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 4,
agency: 'Glasgow Investment',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 5,
agency: 'Actual I.T Solutions',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 6,
agency: 'Oracle Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 7,
agency: 'Oracle Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 8,
agency: 'Pinquest Ventures',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 9,
agency: 'Antiochist Enterprise',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 10,
agency: 'Quantify Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 11,
agency: 'Apple Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 12,
agency: 'Datagin Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 13,
agency: 'InfoMix Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
},
participantDetails: {
id: 14,
agency: 'Sybase Inc.',
agencyPhone: '07083590156',
agencyAltPhone: '07083590156',
agencyEmail: '[email protected]',
testTest: 35,
testInfo:[{
courseNo: '690638HS738/51YG',
centerAddress: '22, Muri Okunola Road, VI-Lagos, Nigeria',
},
{
courseNo: '389474RY738/51YG',
centerAddress: '33, Dangote Lane, Apapa-Lagos, Nigeria',
},
{
courseNo: '93788NB738/51YG',
centerAddress: '11, Harley Grace Drive, VI-Lagos, Nigeria',
},
{
courseNo: '490259FU003/51YG',
centerAddress: '8, Martin Dell Road, VI-Lagos, Nigeria',
},
{
courseNo: '846708GH462/51YG',
centerAddress: '1, Adeola Orlean Road, VI-Lagos, Nigeria',
}],
}
},
},
}];
I have tried the following code but it wasn’t giving me what I want. It was complaining of currentValue variable.
const groupedAgencies = testObj.reduce((accumulator, currentValue) => {
(accumulator[currentValue.testObj.participants.participantDetails.agency] = accumulator[currentValue.testObj.participants.participantDetails.agency] || []).push(currentValue);
}, {});
console.log(`This uses the JS Reduce function to group the record base on a property from the JS object n n The following are Agencies on the TestObj Array Object: n n ${JSON.stringify(groupedAgencies)}`);
What I actually wanted to achieve are the following :
-
Iterate through the testInfo property of the participantDetails of the Participants property of the testObj arrayObject.
-
I want to create or get another object by using the JavaScript Object reduce function to group the Agency property of the participantDetails of participants property of the testObj array object.
I can easily do this from the MongoDB using Mongoose but I actually wanted to get the records off the database and use the Javascript to just do the operation at the frontend or the backend(controller) level with just Javascript code so as to release the database or the backend of long running job and allow the backend and database to serve other incoming request easily.