You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在现有表格数据收集方法中以最简洁方式过滤重复Column Values?

问题:如何在现有表格数据收集方法内对数组类型的Column Values进行去重?

我需要把文件中的Column Values推送至多张表格,但频繁出现重复值影响展示。我希望实现值的唯一显示(仅保留每个相同值的一个实例),此前已在项目中另一张表格完成该去重功能,但当前表格的数据收集方式不同,我不确定如何在现有方法内对Column Values进行去重,而非新建方法,恳请提供技术建议。

以下是相关代码:

当前表格数据收集代码

participantDetails(participant) {
  return [
    // take all column values, move into arrays, take out duplicates,
    { columnName: I18n.t('ccenter.case_file.table.role'), columnValue: participant.role, dataPointName: 'role' },
    { columnName: I18n.t('ccenter.case_file.table.company_names'), columnValue: participant.companyNames.join(', ') || '-', dataPointName: 'companyName' },
    { columnName: I18n.t('ccenter.case_file.table.aliases'), columnValue: participant.aliases.join(', ') || '-', dataPointName: 'name' },
    { columnName: I18n.t('ccenter.case_file.table.birthdates'), columnValue: participant.birthdates.join(', ') || '-', dataPointName: 'birthdate' },
    { columnName: I18n.t('ccenter.case_file.table.phones'), columnValue: participant.phones.join(', ') || '-', dataPointName: 'phone' },
    { columnName: I18n.t('ccenter.case_file.table.emails'), columnValue: participant.emails.join(', ') || '-', dataPointName: 'email' },
    { columnName: I18n.t('ccenter.case_file.table.addresses'), columnValue: participant.addresses.join(', ') || '-', dataPointName: 'address' },
    { columnName: I18n.t('ccenter.case_file.table.customer_numbers'), columnValue: participant.customerNumbers.join(', ') || '-', dataPointName: 'customerNumber' }
  ]
},

此前表格的去重代码

fields.forEach(field => {
  if (participant[field.fieldName].length > 0) {
    const uniqueValues = [];
    const uniqueElements = [];
    participant[field.fieldName].forEach(element => {
      if (!uniqueValues.includes(element.value)) {
        uniqueValues.push(element.value);
        uniqueElements.push(element);
      }
    })
    result.push({
      attributeName: field.fieldName,
      columnName: I18n.t(`ccenter.participant.table.${snakeCase(field.fieldName)}`),
      columnValues: uniqueElements,
      fieldClass: field.fieldClass,
    })
  }
});
return result;

回答

这问题我太懂了!你之前处理的是带value属性的对象数组,而现在当前表格里的participant.companyNames、aliases这些都是纯字符串数组,完全可以在现有participantDetails方法里直接嵌入去重逻辑,不用额外新建方法,改动非常小。

具体实现步骤:

  1. 在participantDetails方法内部定义一个极简的去重辅助函数(也可以用单行写法,但函数更清晰),利用ES6的Set来快速去重字符串数组;
  2. 在每个需要去重的数组属性join之前,先调用这个去重函数。

修改后的完整代码:

participantDetails(participant) {
  // 定义内部去重函数,处理字符串数组
  const getUniqueStrings = (arr) => {
    // 过滤空值(如果需要的话,可根据实际情况调整)
    const filteredArr = arr.filter(item => item);
    // 用Set去重后转回数组
    return [...new Set(filteredArr)];
  };

  return [
    { columnName: I18n.t('ccenter.case_file.table.role'), columnValue: participant.role, dataPointName: 'role' },
    // 对每个数组属性先去重再join
    { columnName: I18n.t('ccenter.case_file.table.company_names'), columnValue: getUniqueStrings(participant.companyNames).join(', ') || '-', dataPointName: 'companyName' },
    { columnName: I18n.t('ccenter.case_file.table.aliases'), columnValue: getUniqueStrings(participant.aliases).join(', ') || '-', dataPointName: 'name' },
    { columnName: I18n.t('ccenter.case_file.table.birthdates'), columnValue: getUniqueStrings(participant.birthdates).join(', ') || '-', dataPointName: 'birthdate' },
    { columnName: I18n.t('ccenter.case_file.table.phones'), columnValue: getUniqueStrings(participant.phones).join(', ') || '-', dataPointName: 'phone' },
    { columnName: I18n.t('ccenter.case_file.table.emails'), columnValue: getUniqueStrings(participant.emails).join(', ') || '-', dataPointName: 'email' },
    { columnName: I18n.t('ccenter.case_file.table.addresses'), columnValue: getUniqueStrings(participant.addresses).join(', ') || '-', dataPointName: 'address' },
    { columnName: I18n.t('ccenter.case_file.table.customer_numbers'), columnValue: getUniqueStrings(participant.customerNumbers).join(', ') || '-', dataPointName: 'customerNumber' }
  ]
},

为什么这么做?

  • 完全复用现有方法结构,不需要新增外部函数或方法,符合你的需求;
  • Set是ES6原生的去重方式,性能比手动遍历判断更好;
  • 额外加了filter(item => item)可以过滤掉数组里的空字符串/undefined(如果你的数据源可能有这类无效值的话,不需要可以删掉);
  • 原来的|| '-'逻辑完全保留,不影响空数组的展示效果。

如果你的数组里是复杂对象(比如和之前的代码一样有value属性),那只需要把getUniqueStrings改成类似之前的逻辑即可,但看当前代码里都是直接join,应该都是纯字符串数组,这个方案完全适用。

内容的提问来源于stack exchange,提问作者cbln1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:12:42