Convert JS data to parent-child node data

the original data is tiled, such as

originalData: [
        { company: "AAA", department:"AAA-D1", office: "AAA-D1-O1", pass: 1, onhold: 3},
        { company: "AAA", department:"AAA-D1", office: "AAA-D1-O2", pass: 3, onhold: 5},
        { company: "AAA", department:"AAA-D2", office: "AAA-D2-O1", pass: 2, onhold: 7},
        { company: "BBB", department:"BBB-D1", office: "BBB-D1-O1", pass: 1, onhold: 3},
        { company: "BBB", department:"BBB-D2", office: "BBB-D2-O1", pass: 4, onhold: 3},
        { company: "BBB", department:"BBB-D3", office: "BBB-D3-O1", pass: 1, onhold: 3},
        ....
        ....
      ]

want to convert to

treeData: [
    { label: "AAA", department:"AAA-D1", office: "AAA-D1-O1", pass: 6, onhold: 15,   // company level 
        children: [  // department level 
            {label: "AAA-D1",  pass: 4, onhold: 8,
                children: [  // office level
                    {label: "AAA-D1-O1", pass: 1, onhold: 3},
                    {label: "AAA-D1-O2", pass: 1, onhold: 3},
                ]
            },
            {label: "AAA-D2",  pass: 2, onhold: 7,
                children: [
                    {label: "AAA-D2-O1", pass: 1, onhold: 3},
                ]
            },
        ]
    },
    ...
    ...
]
    

convert the data into parent-child structure according to the specified column, for example, the above is based on ["company"," department", "office"]. I don"t know how to write it. Please guide: (: (

.
Mar.17,2021

after answering, I found that what you want is to be able to map according to the specified column.

then let me think about it again. The following can be used as a reference, ORZ.

. < hr >

<br>label: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 6, onhold: 15, // company level label: 'AAA', pass: 6, onhold: 15, // company level <br>3

var moriginalData= [
        { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 1, onhold: 3},
        { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O2', pass: 3, onhold: 5},
        { company: 'AAA', department:'AAA-D2', office: 'AAA-D2-O1', pass: 2, onhold: 7},
        { company: 'BBB', department:'BBB-D1', office: 'BBB-D1-O1', pass: 1, onhold: 3},
        { company: 'BBB', department:'BBB-D2', office: 'BBB-D2-O1', pass: 4, onhold: 3},
        { company: 'BBB', department:'BBB-D3', office: 'BBB-D3-O1', pass: 1, onhold: 3}
      ];
function a2o(originalData){
    var outData=[];
    var outObj={};
    for(var i=0;i<originalData.length;iPP){
        var company= originalData[i].company;
        var department= originalData[i].department;
        var office={label:originalData[i].office, pass:originalData[i].pass, onhold:originalData[i].onhold};
        if(outObj[company]===undefined){
            outObj[company]={childrenKey:[], pass:0, onhold:0};
        }
        if(outObj[company][department]===undefined){
            outObj[company][department]={children:[], pass:0, onhold:0 };
            outObj[company].childrenKey.push(department)
        }
        outObj[company][department].children.push(office);

        outObj[company][department].pass=outObj[company][department].pass+office.pass;
        outObj[company].pass=outObj[company].pass+office.pass;

        outObj[company][department].onhold=outObj[company][department].onhold+office.onhold;
        outObj[company].onhold=outObj[company].onhold+office.onhold;
    }
    for( var com in outObj){
        var tmpA={label:com, pass:outObj[com].pass, onhold:outObj[com].onhold, children:[]};
        for (var j=0; j<outObj[com].childrenKey.length; jPP ){
            var tD=outObj[com][ outObj[com].childrenKey[j] ];
            tmpA.children.push({label:outObj[com].childrenKey[j] ,
                                pass:tD.pass , 
                                onhold:tD.onhold ,
                                children:tD.children} );
        }
        outData.push(tmpA);
    }
    return outData;
}   

var treeDate=a2o(moriginalData)
console.log(treeDate);  
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38e38-2c164.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38e38-2c164.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?