I wanted to change my array of object to different format
I have below output,
let result = [
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 2,
"user_id": 101,
"user_name": "test 2",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 3,
"user_id": 102,
"user_name": "test 3",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 4,
"user_id": 103,
"user_name": "test 4",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:34:24.000Z",
"date_updated": "2022-11-03T09:34:24.000Z",
"created_by": "23",
"updated_by": "23",
"team_participants_id": 13,
"user_id": 104,
"user_name": "test 5",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 16,
"user_id": 105,
"user_name": "test 6",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 17,
"user_id": 106,
"user_name": "test 7",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
}
]
And I wanted to convert to below format
let foramtedResponse = [{
"team_details_id": 1,
"team_name": "Avengers",
"participant_list":[{
"user_id": 101,
"user_name": "test 2",
"email_id": "[email protected]",
"role_id": 3
},{
"user_id": 102,
"user_name": "test 3",
"email_id": "[email protected]",
"role_id": 3,
},
{
"user_id": 103,
"user_name": "test 4",
"email_id": "[email protected]",
"role_id": 3
},
{
"user_id": 104,
"user_name": "test 5",
"email_id": "[email protected]",
"role_id": 3
}]
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"participant_list":[{
"user_id": 105,
"user_name": "test 6",
"email_id": "[email protected]",
"role_id": 3
},{
"team_participants_id": 17,
"user_id": 106,
"user_name": "test 7",
"email_id": "[email protected]",
"role_id": 3
}]
}
]
I wanted to do this with the help of map.reduce to reduce to nested array of object and I wanted to match the user_name,user_id and team_details_id for reference. Please help me Thanks in advance
CodePudding user response:
See the following code for a possible solution. You forgot to add the members to the array and you didnt set the team_details_id.
let result = [
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 2,
"user_id": 101,
"user_name": "test 2",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 3,
"user_id": 102,
"user_name": "test 3",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 4,
"user_id": 103,
"user_name": "test 4",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:34:24.000Z",
"date_updated": "2022-11-03T09:34:24.000Z",
"created_by": "23",
"updated_by": "23",
"team_participants_id": 13,
"user_id": 104,
"user_name": "test 5",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 16,
"user_id": 105,
"user_name": "test 6",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 17,
"user_id": 106,
"user_name": "test 7",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
}
]
const reduceResult = (result) =>
result.reduce((data, item) => {
let team_details = data.find(p => p.team_details_id === item.team_details_id);
if (!team_details) {
team_name = {
team_details_id: item.team_details_id,
team_name: item.team_name,
team_participants: [
{
user_id: item.user_id,
user_name: item.user_name,
email_id: item.email_id,
role_id: item.role_id,
}
]
}
data.push(team_name);
} else {
team_details.team_participants.push(
{
user_id: item.user_id,
user_name: item.user_name,
email_id: item.email_id,
role_id: item.role_id,
}
)
}
return data;
}, []);
console.log("reduceResult:-",reduceResult(result));
CodePudding user response:
you are correct in thinking the array.reduce
method is what you need here. First use the reduce method to create an object where the team names are the keys and all the players are added into this object. Then use the Object.values
method to transform the object into an array and it matches your desired data structure.
let result = [
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 2,
"user_id": 101,
"user_name": "test 2",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 3,
"user_id": 102,
"user_name": "test 3",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:31:13.000Z",
"date_updated": "2022-11-03T09:31:13.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 4,
"user_id": 103,
"user_name": "test 4",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 1,
"team_name": "Avengers",
"team_description": null,
"date_created": "2022-11-03T09:34:24.000Z",
"date_updated": "2022-11-03T09:34:24.000Z",
"created_by": "23",
"updated_by": "23",
"team_participants_id": 13,
"user_id": 104,
"user_name": "test 5",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 16,
"user_id": 105,
"user_name": "test 6",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
},
{
"team_details_id": 5,
"team_name": "KantaraBuilders",
"team_description": null,
"date_created": "2022-11-03T09:35:23.000Z",
"date_updated": "2022-11-03T09:35:23.000Z",
"created_by": "23",
"updated_by": null,
"team_participants_id": 17,
"user_id": 106,
"user_name": "test 7",
"email_id": "[email protected]",
"role_id": 3,
"is_active": 1,
"access_code": null
}
]
const dataInObjectStructure = result.reduce((agg, item) => {
if (agg[item.team_name]) {
agg[item.team_name].participant_list.push(
{
user_id: item.user_id,
user_name: item.user_name,
email_id: item.email_id,
role_id: item.role_id,
}
)
} else {
agg[item.team_name] = {
team_details_id: item.team_details_id,
team_name: item.team_name,
participant_list:[{
user_id: item.user_id,
user_name: item.user_name,
email_id: item.email_id,
role_id: item.role_id,
}]
}
}
return agg
}, {})
const dataInArrayFormat = Object.values(dataInObjectStructure);
console.log(dataInArrayFormat)