如何用map.reduce将团队对象数组转换为嵌套分组格式?
问题
需要将给定的对象数组转换为指定格式,要求使用map+reduce方法实现,以user_name、user_id和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": "test2@gmail.com", "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": "test3@gmail.com", "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": "test4@gmail.com", "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": "test5@gmail.com", "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": "test6@gmail.com", "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": "test7@gmail.com", "role_id": 3, "is_active": 1, "access_code": null } ];
期望格式
let formattedResponse = [{ "team_details_id": 1, "team_name": "Avengers", "participant_list":[{ "user_id": 101, "user_name": "test 2", "email_id": "test2@gmail.com", "role_id": 3 },{ "user_id": 102, "user_name": "test 3", "email_id": "test3@gmail.com", "role_id": 3 }, { "user_id": 103, "user_name": "test 4", "email_id": "test4@gmail.com", "role_id": 3 }, { "user_id": 104, "user_name": "test 5", "email_id": "test5@gmail.com", "role_id": 3 }] }, { "team_details_id": 5, "team_name": "KantaraBuilders", "participant_list":[{ "user_id": 105, "user_name": "test 6", "email_id": "test6@gmail.com", "role_id": 3 },{ "user_id": 106, "user_name": "test 7", "email_id": "test7@gmail.com", "role_id": 3 }] }];
解决方案
实现思路
- 用
reduce按team_details_id分组,将同一团队的参与者聚合到participant_list - 依据
user_id和user_name过滤重复参与者 - 用
Object.values()将分组后的键值对对象转为目标数组格式
代码实现
const formattedResponse = Object.values(result.reduce((acc, item) => { const teamId = item.team_details_id; // 初始化团队信息(如果不存在) if (!acc[teamId]) { acc[teamId] = { team_details_id: teamId, team_name: item.team_name, participant_list: [] }; } // 检查参与者是否已存在,避免重复添加 const isDuplicate = acc[teamId].participant_list.some(p => p.user_id === item.user_id && p.user_name === item.user_name ); if (!isDuplicate) { acc[teamId].participant_list.push({ user_id: item.user_id, user_name: item.user_name, email_id: item.email_id, role_id: item.role_id }); } return acc; }, {})); console.log(formattedResponse);
代码说明
- 分组逻辑:以
team_details_id为键,将同一团队的所有条目聚合到同一个对象下 - 去重逻辑:通过
some方法校验user_id和user_name,避免重复添加同一用户 - 格式转换:
Object.values()将reduce生成的键值对结构转为数组,匹配目标格式
内容的提问来源于stack exchange,提问作者Pankaj Dhumal
相关产品推荐
相关产品推荐

