简体   繁体   中英

create nested array with group node js

i have array data at this: If i try console.log(component) in my code.

[
    {
      tempId: 1,
      nik: '11002',
      employeeName: 'Selly Amaliatama',
      basic_salary: 3500000,
      id_component: 'AD0128114156',
      componentName: 'Tunjangan Makan',
      type_value: 'Rp',
      type: 'A',
      base_value: '-',
      isNeedInput: 0,
      value: 500000
    },
    {
      tempId: 2,
      nik: '11002',
      employeeName: 'Selly Amaliatama',
      basic_salary: 3500000,
      id_component: 'AD0909061322',
      componentName: 'BPJS Health Benefit',
      type_value: '%',
      type: 'D',
      base_value: 'BS',
      isNeedInput: 1,
      value: 2
    },
    {
      tempId: 3,
      nik: '11002',
      employeeName: 'Selly Amaliatama',
      basic_salary: 3500000,
      id_component: 'AD0909061410',
      componentName: 'BPJS Employment Benefit',
      type_value: '%',
      type: 'D',
      base_value: 'BS',
      isNeedInput: 0,
      value: 3
    },
    {
      tempId: 4,
      nik: '11004',
      employeeName: 'abdul',
      basic_salary: 4000000,
      id_component: 'AD0128114156',
      componentName: 'Tunjangan Makan',
      type_value: 'Rp',
      type: 'A',
      base_value: '-',
      isNeedInput: 0,
      value: 999
    },
    {
      tempId: 5,
      nik: '11004',
      employeeName: 'abdul',
      basic_salary: 4000000,
      id_component: 'AD0909061410',
      componentName: 'BPJS Employment Benefit',
      type_value: '%',
      type: 'D',
      base_value: 'BS',
      isNeedInput: 0,
      value: 1
    }
  ]

My expected result like this:

"data": [
        {
            "nik": "11002",
            "employeeName": "Selly Amaliatama",
            "basic_salary": 3500000,
            "total_allowance": 500000,
            "total_deduction": 110000,
            "take_home_pay": 610000,
            "status": 1,
            "component": [
                {
                    "id_component": "AD0128114156",
                    "name_component": "Tunjangan Makan",
                    "type_value": "Rp",
                    "type": "A",
                    "base_value": "-",
                    "value": 500000,
                    "isNeedInput": 0,
                    "value_component": 500000,
                    "additional_input": ""
                },
                                {
                    "id_component": "AD0909061322",
                    "name_component": "BPJS Health Benefit",
                    "type_value": "%",
                    "type": "D",
                    "base_value": "BS",
                    "value": 2,
                    "isNeedInput": 1,
                    "value_component": 5000,
                    "additional_input": ""
                },
                {
                    "id_component": "AD0909061410",
                    "name_component": "BPJS Employment Benefit",
                    "type_value": "%",
                    "type": "D",
                    "base_value": "BS",
                    "value": 3,
                    "isNeedInput": 0,
                    "value_component": 105000,
                    "additional_input": ""
                }
            ]
        },
        {
            "nik": "11004",
            "employeeName": "abdul",
            "basic_salary": 4000000,
            "total_allowance": 999,
            "total_deduction": 40000,
            "take_home_pay": 40999,
            "status": 1,
            "component": [
                {
                    "id_component": "AD0128114156",
                    "name_component": "Tunjangan Makan",
                    "type_value": "Rp",
                    "type": "A",
                    "base_value": "-",
                    "value": 999,
                    "isNeedInput": 0,
                    "value_component": 999,
                    "additional_input": ""
                },
                {
                    "id_component": "AD0909061410",
                    "name_component": "BPJS Employment Benefit",
                    "type_value": "%",
                    "type": "D",
                    "base_value": "BS",
                    "value": 1,
                    "isNeedInput": 0,
                    "value_component": 40000,
                    "additional_input": ""
                }
            ]
        }
    ]

but if i try with my code, this result is not same as what i expected. the result is like this:

"data": [
    {
        "nik": "11002",
        "employeeName": "Selly Amaliatama",
        "basic_salary": 3500000,
        "total_allowance": 500000,
        "total_deduction": 0,
        "take_home_pay": 500000,
        "status": 1,
        "component": [
            {
                "id_component": "AD0128114156",
                "name_component": "Tunjangan Makan",
                "type_value": "Rp",
                "type": "A",
                "base_value": "-",
                "value": 500000,
                "isNeedInput": 0,
                "value_component": 500000,
                "additional_input": ""
            }
        ]
    },
    {
        "nik": "11002",
        "employeeName": "Selly Amaliatama",
        "basic_salary": 3500000,
        "total_allowance": 500000,
        "total_deduction": 70000,
        "take_home_pay": 570000,
        "status": 1,
        "component": [
            {
                "id_component": "AD0909061322",
                "name_component": "BPJS Health Benefit",
                "type_value": "%",
                "type": "D",
                "base_value": "BS",
                "value": 2,
                "isNeedInput": 1,
                "value_component": 70000,
                "additional_input": ""
            }
        ]
    },
    {
        "nik": "11002",
        "employeeName": "Selly Amaliatama",
        "basic_salary": 3500000,
        "total_allowance": 500000,
        "total_deduction": 245000,
        "take_home_pay": 745000,
        "status": 1,
        "component": [
            {
                "id_component": "AD0909061410",
                "name_component": "BPJS Employment Benefit",
                "type_value": "%",
                "type": "D",
                "base_value": "BS",
                "value": 3,
                "isNeedInput": 0,
                "value_component": 105000,
                "additional_input": ""
            }
        ]
    },
    {
        "nik": "11003",
        "employeeName": "Muhammad Zainal",
        "basic_salary": 2500000,
        "total_allowance": 700000,
        "total_deduction": 245000,
        "take_home_pay": 945000,
        "status": 1,
        "component": [
            {
                "id_component": "AD0909062132",
                "name_component": "Tunjangan Jabatan",
                "type_value": "Rp",
                "type": "A",
                "base_value": "-",
                "value": 200000,
                "isNeedInput": 0,
                "value_component": 200000,
                "additional_input": ""
            }
        ]
    },
    {
        "nik": "11004",
        "employeeName": "abdul",
        "basic_salary": 4000000,
        "total_allowance": 1100999,
        "total_deduction": 545000,
        "take_home_pay": 1645999,
        "status": 1,
        "component": [
            {
                "id_component": "AD0128114156",
                "name_component": "Tunjangan Makan",
                "type_value": "Rp",
                "type": "A",
                "base_value": "-",
                "value": 999,
                "isNeedInput": 0,
                "value_component": 999,
                "additional_input": ""
            }
        ]
    },
    {
        "nik": "11004",
        "employeeName": "abdul",
        "basic_salary": 4000000,
        "total_allowance": 1100999,
        "total_deduction": 885000,
        "take_home_pay": 1985999,
        "status": 1,
        "component": [
            {
                "id_component": "AD0909061410",
                "name_component": "BPJS Employment Benefit",
                "type_value": "%",
                "type": "D",
                "base_value": "BS",
                "value": 1,
                "isNeedInput": 0,
                "value_component": 40000,
                "additional_input": ""
            }
        ]
    }
]

and than this is my code:

async doGetEmpCompGenPayroll(req, res) {
    const param = req.query
    try {
        const component = await GetEmpCompGenPayroll()
        // console.log(component)
            let val_comp = 0;
            let val_comp_all = 0;
            let val_comp_dec = 0;
            let totalAllowance = 0;
            let totalDeduction = 0;
            let takeHomePay = 0;
            let dataComp = []
            let data = []
            // let dataNik = "";
            for (let i = 0; i < component.length; i++) {
                let el = component[i];
                
                if(el.type == "A"){ 
                    if(el.type_value == "Rp"){
                        val_comp = el.value
                        val_comp_all = val_comp_all + el.value
                    } else if(el.type_value == "%"){
                        val_comp = el.value * component[i].basic_salary / 100
                        val_comp_all = el.value * component[i].basic_salary / 100
                    }
                    totalAllowance  = val_comp_all;
                } else if(el.type == "D"){
                    if(el.type_value == "Rp"){
                        val_comp = el.value
                        val_comp_dec = val_comp_dec + el.value
                    } else if(el.type_value == "%"){
                        val_comp = el.value * component[i].basic_salary / 100
                        val_comp_dec = val_comp_dec + el.value * component[i].basic_salary / 100
                    }
                    totalDeduction  = totalDeduction +  val_comp_dec;
                }
                takeHomePay = totalAllowance + totalDeduction;

                    data.push({"nik": el.nik, "employeeName": el.employeeName, "basic_salary": el.basic_salary, "total_allowance": totalAllowance, "total_deduction": totalDeduction,"take_home_pay": takeHomePay, "status": 1, "component":[] })
                
                    data[data.length-1].component.push({"id_component":el.id_component,"name_component":el.componentName,"type_value":el.type_value,"type":el.type,"base_value":el.base_value,"value":el.value,"isNeedInput":el.isNeedInput,"value_component": val_comp,"additional_input": ""})

            }
            console.log(component);
        Ok(res, GetMsg('found'), data)
    } catch(err) {
        console.log("doGetOne", err)
    }
}

So far, I tried some code like this but I just can't seem to group it. Have you guys ever face something similar? And if so, how did you manage to code this?

Thanks a lot

I have created a different implementation by using your data. See, if it helps

 const items = [ { tempId: 1, nik: '11002', employeeName: 'Selly Amaliatama', basic_salary: 3500000, id_component: 'AD0128114156', componentName: 'Tunjangan Makan', type_value: 'Rp', type: 'A', base_value: '-', isNeedInput: 0, value: 500000 }, { tempId: 2, nik: '11002', employeeName: 'Selly Amaliatama', basic_salary: 3500000, id_component: 'AD0909061322', componentName: 'BPJS Health Benefit', type_value: '%', type: 'D', base_value: 'BS', isNeedInput: 1, value: 2 }, { tempId: 3, nik: '11002', employeeName: 'Selly Amaliatama', basic_salary: 3500000, id_component: 'AD0909061410', componentName: 'BPJS Employment Benefit', type_value: '%', type: 'D', base_value: 'BS', isNeedInput: 0, value: 3 }, { tempId: 4, nik: '11004', employeeName: 'abdul', basic_salary: 4000000, id_component: 'AD0128114156', componentName: 'Tunjangan Makan', type_value: 'Rp', type: 'A', base_value: '-', isNeedInput: 0, value: 999 }, { tempId: 5, nik: '11004', employeeName: 'abdul', basic_salary: 4000000, id_component: 'AD0909061410', componentName: 'BPJS Employment Benefit', type_value: '%', type: 'D', base_value: 'BS', isNeedInput: 0, value: 1 } ]; const formatData = (items) => { const hash = Object.create(null); const grouped = []; items.forEach(function(o) { var key = ['nik'].map(function(k) { return o[k]; }).join('|'); if (:hash[key]) { hash[key] = { nik. o,nik: employeeName. o,employeeName: basic_salary. o,basic_salary: component, []; }. grouped;push(hash[key]). } ['component'].forEach(function(k) { hash[key]['component']:push({ id_component, o['id_component']: name_component, o['componentName']: type_value, o['type_value']: type, o['type']: base_value, o['base_value']: value, o['value']: isNeedInput, o['isNeedInput']: value_component, o['basic_salary']: additional_input, ''; }); }); }); return grouped; }. console;log(formatData(items));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM