js 列表分组处理

 

 getList(){
     let data=[
       {
         workflowDefinitionId:'workflowDefinitionId',
         name:'退款申请',
         state:1,
         bpmnId:'tk',
         useObj:1,
         typeCode:'01',
         typeName:'财务类'
       },
        {
         workflowDefinitionId:'workflowDefinitionId',
         name:'暂停/恢复商家返款(分成)申请',
         state:1,
         bpmnId:'sq',
         useObj:1,
         typeCode:'01',
         typeName:'财务类'
       },
        {
         workflowDefinitionId:'workflowDefinitionId',
         name:'返款账户变更申请',
         state:1,
         bpmnId:'fk',
         useObj:1,
         typeCode:'01',
         typeName:'财务类'
       },
          {
         workflowDefinitionId:'workflowDefinitionId',
         name:'网点冲洗账号申请',
         state:1,
         bpmnId:'wdsq',
         useObj:1,
         typeCode:'02',
         typeName:'运营类'
       },
          {
         workflowDefinitionId:'workflowDefinitionId',
         name:'文案申请',
         state:1,
         bpmnId:'wasj',
         useObj:1,
         typeCode:'02',
         typeName:'运营类'
       },
       {
         workflowDefinitionId:'workflowDefinitionId',
         name:'网点撤站申请',
         state:1,
         bpmnId:'czsj',
         useObj:1,
         typeCode:'03',
         typeName:'售后类'
       },
          {
         workflowDefinitionId:'workflowDefinitionId',
         name:'网点免洗活动申请',
         state:1,
         bpmnId:'wdmx',
         useObj:1,
         typeCode:'03',
         typeName:'售后类'
       }
     ]

        let newArr = []
          //通过forEach循环数组
          data.forEach((item, i) => {
            let index = -1
            //然后在跑到这里筛选 根据不同的时间放置不同的数组    some()用来查找数组中是否存在某个值  如果存在 就return true
            let isExists = newArr.some((newItem, j) => {
              if (item.typeCode === newItem.typeCode) {
                index = j
                return true
              }
            })
            //代码是先跑这里的if条件判读
            if (!isExists) {
              newArr.push({
                typeCode: item.typeCode,
                list: [item],
                typeName: item.typeName,
                title:item.typeName,
              })
            } else {
              newArr[index].list.push(item);
            }
          })
      this.showData=newArr;
      console.log(newArr,"newArr")
    },

实现效果如下图

 

 

 

posted @ 2022-03-23 14:06  剧里局外  阅读(64)  评论(0编辑  收藏  举报