首页 > 解决方案 > 使用组节点js创建嵌套数组

问题描述

我有数组数据:如果我在我的代码中尝试 console.log(component) 。

[
    {
      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
    }
  ]

我的预期结果是这样的:

"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": ""
                }
            ]
        }
    ]

但是如果我尝试使用我的代码,这个结果与我的预期不同。结果是这样的:

"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": ""
            }
        ]
    }
]

这是我的代码:

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)
    }
}

到目前为止,我尝试了一些这样的代码,但我似乎无法对它进行分组。大家有没有遇到过类似的情况?如果是这样,您是如何编写代码的?

非常感谢

标签: javascriptnode.jsarraysobjectnested

解决方案


我使用您的数据创建了一个不同的实现。看看有没有帮助

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));


推荐阅读