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

如何使用JavaScript从数组中按优先级统计工单数量并按坐席分组

代码调整方案

你原有代码的核心问题是Map的键选择错误,且没有对两个目标优先级分别设置计数规则,调整后可直接实现需求:

const agentStatsMap = new Map();

for (let i = 0; i < dataModel.ticketList.length; i++) {
  const ticket = dataModel.ticketList[i];
  // 请根据你实际数据的字段大小写调整,示例和你给出的输入字段保持一致
  const agentId = ticket.agent_id;
  const priority = ticket.priority;

  // 坐席首次出现时初始化统计项,默认两个优先级计数都为0
  if (!agentStatsMap.has(agentId)) {
    agentStatsMap.set(agentId, {
      agent_id: agentId,
      High_Count: 0,
      Medium_Count: 0
    });
  }

  // 按当前工单的优先级累加计数
  const stat = agentStatsMap.get(agentId);
  if (priority === 'High') stat.High_Count++;
  if (priority === 'Medium') stat.Medium_Count++;
}

const res = Array.from(agentStatsMap.values());
return res;

注意事项

  • 如果你的实际数据字段是首字母大写的Agent、Priority,对应把取值部分的字段名修改为大写开头即可
  • 不需要对priority做JSON.stringify处理,本身就是字符串类型可直接判断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:48:02