VUE 分组取最大,生成子集合
// 分组函数
const groupByAge = (docs: TOSDepositoryDTO[]): Record<number, TOSDepositoryDTO[]> => {
return docs.reduce((acc, d) => {
if (!acc[d.approvalId]) acc[d.approvalId] = [];
acc[d.approvalId].push(d);
return acc;
}, {} as Record<number, TOSDepositoryDTO[]>);
};
// 获取每组最大用户和其他用户的函数
const getMaxUserWithOthersByAge = (docs: TOSDepositoryDTO[]) => {
const groupedUsers = groupByAge(docs);
return Object.entries(groupedUsers).map(([approvalId, usersGroup]) => {
// 根据 id 获取最大用户
const maxUser = usersGroup.reduce((max, current) => (current.revisionNumber > max.revisionNumber ? current : max));
maxUser.items = usersGroup.filter(user => user.id !== maxUser.id); // 过滤掉最大用户,生成其他用户的子数组
return maxUser;
});
};
const getTOSDepository = async () => {
var resDoc = await ClientService.getTOSDepository(model.value.id);
documents.value = getMaxUserWithOthersByAge(resDoc);
console.log(documents.value);
}
const sectorList = ref<SectorDTO[]>();
const industrySectorList = ref<SectorDTO[]>();
sectorList.value = industrySectorList.value?.filter(m => selectIndustrys.value?.includes(m.industryID))
watch([selectIndustrys], () => {
console.log(3311111, selectIndustrys.value);
sectorList.value = industrySectorList.value?.filter(m => selectIndustrys.value?.includes(m.industryID))
// if (selectIndustrys.value)
// IndustryList.value?.filter(m => selectIndustrys.value?.includes(m.industryID)).forEach(i => { console.log(111, i.items); sectorList.value?.concat(i.items) })
})
IndustryList.value = industrySectorList.value.reduce<SectorDTO[]>((accumulator, current) => {
const exists = accumulator.some(item => item.industryName === current.industryName && item.industryID === current.industryID);
if (!exists) accumulator.push(current); console.log(5555, current);
return accumulator;
}, []);