cfListvalue = await defHttpget url workstationylYncfcflistByPharmacy params formData ;cfList如何根据每条数据的nameage和sex统计并展示同一name且同一age同一sex的数据条数并将其赋值给const patientList = ref;并将这三项数据相同的数据的id合到一起赋值给patientLi
您可以使用数组的reduce方法来实现统计和合并操作。具体代码如下:
const patientList = ref([]);
const cfList = await defHttp.get({ url: '/workstation/ylYncf/cflistByPharmacy', params: formData });
const groupedData = cfList.value.reduce((result, item) => {
const key = `${item.name}-${item.age}-${item.sex}`;
if (!result[key]) {
result[key] = { count: 0, ids: [] };
}
result[key].count++;
result[key].ids.push(item.id);
return result;
}, {});
for (const key in groupedData) {
const [name, age, sex] = key.split('-');
const { count, ids } = groupedData[key];
patientList.value.push({ name, age, sex, count, ids });
}
console.log(patientList.value);
上述代码首先定义了一个空数组patientList,然后使用reduce方法对cfList进行遍历,将数据按照name、age和sex进行分组统计和合并,并将结果存储在groupedData对象中。最后,将groupedData中的结果转化为所需的格式,存储在patientList数组中。最终,打印patientList数组的值。
请注意,在上述代码中,假设cfList是一个ref对象,因此需要通过.value来获取其实际值。另外,为了能够使用reduce方法,需要将cfList对象转化为一个普通数组,可以通过.value获取其值后进行转化
原文地址: http://www.cveoy.top/t/topic/iZLA 著作权归作者所有。请勿转载和采集!