【发布时间】:2021-07-30 14:56:25
【问题描述】:
我有数组数据: 如果我在我的代码中尝试 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)
}
}
到目前为止,我尝试了一些这样的代码,但我似乎无法对其进行分组。 大家有没有遇到过类似的情况?如果是这样,您是如何编写代码的?
非常感谢
【问题讨论】:
-
“我正在尝试将具有多个这样的属性的大型嵌套对象分组”什么是逻辑?
-
这是我的预期结果,先生,我想按 nik 分组
-
阅读this article 了解调试代码的技巧。
标签: javascript node.js arrays object nested