您可以使用数组的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进行遍历,将数据按照nameagesex进行分组统计和合并,并将结果存储在groupedData对象中。最后,将groupedData中的结果转化为所需的格式,存储在patientList数组中。最终,打印patientList数组的值。

请注意,在上述代码中,假设cfList是一个ref对象,因此需要通过.value来获取其实际值。另外,为了能够使用reduce方法,需要将cfList对象转化为一个普通数组,可以通过.value获取其值后进行转化


原文地址: http://www.cveoy.top/t/topic/iZLA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录