cfListvalue = await defHttpget url workstationylYncfcflistByPharmacy params formData ;cfList如何根据每条数据的nameage和sex统计并展示同一name且同一age同一sex的数据条数并将其赋值给const patientList = ref;并将每条数据的id也一起赋值
您可以使用Array.reduce()方法对cfList进行遍历,根据每条数据的name、age和sex属性进行分组,统计每组中的数据条数,并将结果赋值给patientList。同时,您可以将每条数据的id也一起存储在patientList中。以下是示例代码:
const patientList = ref([]);
// 遍历cfList,根据name、age和sex属性进行分组统计
cfList.value.reduce((acc, curr) => {
// 生成分组的key
const key = `${curr.name}-${curr.age}-${curr.sex}`;
// 如果分组不存在,则创建新的分组
if (!acc[key]) {
acc[key] = {
count: 1,
data: [],
};
} else {
// 分组存在则累加计数
acc[key].count++;
}
// 将当前数据的id保存到分组中
acc[key].data.push(curr.id);
return acc;
}, {}).forEach((group) => {
// 将分组转换为需要的数据格式,并添加到patientList中
patientList.value.push({
name: group.data[0].name,
age: group.data[0].age,
sex: group.data[0].sex,
count: group.count,
ids: group.data,
});
});
// 输出结果
console.log(patientList.value);
请注意,上述代码假设cfList是一个ref类型的响应式数据。如果cfList不是ref类型,您可以省略value属性的使用
原文地址: http://www.cveoy.top/t/topic/iZJ6 著作权归作者所有。请勿转载和采集!