SOURCE

let structVO = [
    {
        "cellId": 45,
        "fieldType": "varchar",
        "fieldName": "单位",
        "show": 1,
        "search": 0,
        "multiLabel": "",
        "sno": 3,
        subtotal:1,
        total:1,
        "cellType": 1,
        "cellCode": "company",
        "dataType": "varchar",
        "dataLength": 10,
        "dataPrecision": null,
        "dataValues": [
            {
                "dataValue": "gdCompany",
                "dataTxt": "广东公司"
            },
            {
                "dataValue": "hnCompany",
                "dataTxt": "湖南公司"
            }
        ]
    },
    {
        "cellId": 2,
        "fieldType": "decimal",
        "fieldName": "银行",
        "show": 1,
        "search": 0,
        "multiLabel": "",
        "sno": 2,
        subtotal:1,
        total:0,
        "cellType": 2,
        "cellCode": "bank",
        "dataType": "varchar",
        "dataLength": 6,
        "dataPrecision": 1,
        "dataValues": [
            {
                "dataValue": "zgBank",
                "dataTxt": "中国银行"
            },
            {
                "dataValue": "gfBank",
                "dataTxt": "广发银行"
            }
        ]
    },
    {
        "cellId": 84,
        "fieldType": "date",
        "fieldName": "到期月份",
        "show": 1,
        "search": 0,
        "multiLabel": "",
        "sno": 5,
        subtotal:0,
        total:0,
        "cellType": 1,
        "cellCode": "edate",
        "dataType": "date",
        "dataLength": null,
        "dataPrecision": null,
        "dataValues": [
            {
                "dataValue": "2022-08",
                "dataTxt": "2022-08"
            },
            {
                "dataValue": "2022-09",
                "dataTxt": "2022-09"
            },
            {
                "dataValue": "2022-10",
                "dataTxt": "2022-10"
            },
        ]
    },
    {
        "cellId": 49,
        "fieldType": "curr",
        "fieldName": "票面金额",
        "show": 1,
        "search": 0,
        "multiLabel": "",
        "sno": 2,
        subtotal:0,
        total:0,
        "cellType": 2,
        "cellCode": "paper_money",
        "dataType": "decimal",
        "dataLength": 13,
        "dataPrecision": 2,
        "dataValues": null
    },
    {
        "cellId": 51,
        "fieldType": "curr",
        "fieldName": "保证金",
        "show": 1,
        "search": 0,
        "multiLabel": "",
        "sno": 3,
        subtotal:0,
        total:0,
        "cellType": 2,
        "cellCode": "deposit",
        "dataType": "decimal",
        "dataLength": 13,
        "dataPrecision": 2,
        "dataValues": null
    }
]

function handleTableHeader(structureResVOs) {
      structureResVOs.sort((a, b) => {
        return a.sno - b.sno
      })
      let headerMap = {}
      for (let rec of structureResVOs) {
        if (!rec.multiLabel) {
          headerMap[rec.fieldName] = { field: rec.cellCode }
        } else {
          headerMap[rec.multiLabel] = headerMap[rec.multiLabel] || []
          headerMap[rec.multiLabel].push({
            headerName: rec.fieldName,
            field: rec.cellCode
          })
        }
      }
      return headerMap
    }
let headerMap = handleTableHeader(structVO)
console.log(headerMap)
let rowData = [
{ id: 1, company: '广东公司',       bank: '中国银行',     edate: '2022-08', paper_money: '1000',deposit:'200' },
{ id: 2, company: '',               bank: '',            edate: '2022-09', paper_money: '1000',deposit:'150' },
{        company: '广东公司小计',    bank: '',            edate: '',         paper_money: '2000',deposit:'350' },
{ id: 6, company: '湖南公司',        bank: '',            edate: '2022-08', paper_money: '200' ,deposit:'500'},
{ id: 7, company: '',               bank: '',            edate: '2022-09', paper_money: '200' ,deposit:'500'},
{        company: '湖南公司小计',    bank: '',            edate: '',         paper_money: '400' ,deposit:'1000'},
{        company: '',                bank: '中国银行小计', edate: '',        paper_money: '2400',deposit:'1350' },
{ id: 3, company: '广东公司',        bank: '广发银行',    edate: '2022-08', paper_money: '1200',deposit:'100' },
{ id: 4, company: '',               bank: '',            edate: '2022-09', paper_money: '1200',deposit:'390' },
{        company: '广东公司小计',    bank: '',            edate: '',        paper_money: '2400',deposit:'490' },
{ id: 8, company: '湖南公司',       bank: '',            edate: '2022-08', paper_money: '100' ,deposit:'350'},
{ id: 9, company: '',              bank: '',             date: '2022-09', paper_money: '100' ,deposit:'350'},
{        company: '湖南公司小计',   bank: '',            edate: '',        paper_money: '200' ,deposit:'700'},
{        company: '',              bank: '广发银行小计', edate: '',        paper_money: '2600',deposit:'1190' },   
]

let tableData = [
    { id: 1, company: '广东公司', bank: '中国银行', edate: '2022-08', paper_money: '1000',deposit:'200' },
    { id: 2, company: '广东公司', bank: '中国银行', edate: '2022-09', paper_money: '1000',deposit:'150' },
    { id: 3, company: '广东公司', bank: '广发银行', edate: '2022-08', paper_money: '1200',deposit:'100' },
    { id: 4, company: '广东公司', bank: '广发银行', edate: '2022-09', paper_money: '1200',deposit:'390' },
    // { id: 5, company: '广东公司', bank: '广发银行', edate: '2022-10', paper_money: '2200',deposit:'110' },
    { id: 6, company: '湖南公司', bank: '中国银行', edate: '2022-08', paper_money: '200' ,deposit:'500'},
    { id: 7, company: '湖南公司', bank: '中国银行', edate: '2022-09', paper_money: '200' ,deposit:'500'},
    { id: 8, company: '湖南公司', bank: '广发银行', edate: '2022-08', paper_money: '100' ,deposit:'350'},
    { id: 9, company: '湖南公司', bank: '广发银行', edate: '2022-09', paper_money: '100' ,deposit:'350'},
    // { id: 10, company: '湖南公司', bank: '广发银行', edate: '2022-10', paper_money: '200' ,deposit:'100'},
]

let map = {}

for(let rec of tableData){
    map[rec.bank] = map[rec.bank] || {}
    map[rec.bank].children =  map[rec.bank].children||{}
    map[rec.bank].children[rec.company] = map[rec.bank].children[rec.company] || []
    map[rec.bank].children[rec.company].push(rec)
}
// console.log(tableData)
console.log(map)
const numField = ['paper_money','deposit']
const notNumField = ['company','bank','edate']

const obj = {}
// notNumField.forEach(item=>obj[item]='')
// for(let key in map){
//     map[key].total = []
//         for(let cKey in map[key].children ){
//         let a = map[key].children[cKey].reduce((pre,cur)=>{         
//             numField.forEach(e=>{
//                 pre[e] = pre[e]||0
//                 pre[e] =Number(pre[e]) +Number(cur[e])
//             })
//             return pre
//         },{})
//         a.bank=`${cKey}小计`
//         a = Object.assign({},obj,a)
//         console.log(a)
//         map[key].total.push(a)
//         map[key].children[cKey].push(a)
//     }
//     map[key].total = map[key].total.reduce((pre,cur)=>{
//         numField.forEach(e=>{
//                 pre[e] = pre[e]||0
//                 pre[e] =Number(pre[e]) +Number(cur[e])
//             })
//             return pre
//     },{})
//     map[key].total = Object.assign({},obj,map[key].total)
// }
console 命令行工具 X clear

                    
>
console