葬空吟 2023-04-20 17:08 采纳率: 61.3%
浏览 31
已结题

js中的数据处理,按条件合并字段内容

如下有val数组,如何处理,能够将modeldefect相同的内容json 合并,并且将checkpoint 字段处理成内容合并,用" | "分格,将level_score,num_score,repeat_score,cycle_score字段处理成新的数组字段,total_score字段做和

val = [
{
    production_date: '2023-04-18',
    checkpoint: 'ZP',
    model: 'one',
    defect: '坑',
    dept: 'AAA',
    level_score: '',
    num_score: '',
    repeat_score: 'ZP复发',
    cycle_score: '',
    total_score: 25
  },
{
    production_date: '2023-04-18',
    checkpoint: 'ZQ',
    model: 'two',
    defect: '包',
    dept: 'AAA',
    level_score: '',
    num_score: '',
    repeat_score: 'ZQ复发',
    cycle_score: '',
    total_score: 25
  },
{
    production_date: '2023-04-18',
    checkpoint: 'ZW',
    model: 'one',
    defect: '坑',
    dept: 'AAA',
    level_score: 'ZW等级',
    num_score: '',
    repeat_score: 'ZW复发',
    cycle_score: '',
    total_score: 45
  },
]

生成结果:


new_val = [
{
    production_date: '2023-04-18',
    checkpoint: 'ZP | ZW',
    model: 'one',
    defect: '坑',
    dept: 'AAA',
    tags:[ 'ZW等级', 'ZP复发', 'ZW复发'],
    total_score: 70
  },
{
    production_date: '2023-04-18',
    checkpoint: 'ZQ',
    model: 'two',
    defect: '包',
    dept: 'AAA',
    tags:['ZQ复发'],
    total_score: 25
  },
]
  • 写回答

4条回答 默认 最新

  • 知昂張呀 2023-04-20 17:15
    关注

    这不就遍历就完事了吗?model 和 defect 的值相同的就该求和的求和 该字符串拼接的拼接 该转数组转数组

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月20日
  • 创建了问题 4月20日