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

JavaScript如何统计数组中去除日期后缀的FileName分类出现次数

问题原因

你的原有代码存在两个问题,导致无法得到正确结果:

  • 直接将数组内的对象元素作为统计对象的key,对象会被隐式转换为[object Object]字符串,所有元素都会被统计到同一个key下
  • 没有提取FileName字段中下划线前的前缀作为统计维度

可行解决方案

// 原始数组赋值给arr变量
const arr = [/* 你的原始数据 */]

const countMap = {}
// 统计每个文件名前缀的出现次数
arr.forEach(item => {
  // 提取FileName下划线前的前缀,忽略后面的时间后缀
  const filePrefix = item.FileName.split('_')[0]
  countMap[filePrefix] = (countMap[filePrefix] || 0) + 1
})

// 转换为要求的输出格式
const output = Object.entries(countMap).map(([prefix, count]) => ({
  FileName: prefix,
  [`${prefix}_Count`]: String(count)
}))

// 输出结果
console.log(output)

运行上述代码后,output变量的值就是你要求的输出格式。

内容的提问来源于stack exchange,提问作者K.D

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 17:30:05