對(duì)象數(shù)組根據(jù)某一相同項(xiàng)示例為travelerEmpNum
,合并其他屬性為一個(gè)數(shù)組粥航,主要包括對(duì)象數(shù)組根據(jù)某一相同項(xiàng)合并其他屬性為一個(gè)數(shù)組纺裁,為項(xiàng)目需求诅病。
對(duì)象數(shù)組示例
let rows = [
{
"travelerName":"王曉&陳辰",
"travelerEmpNum":"1001&1002",
"expenseAmountTotal":1381,
"invoiceAmountTotal":1381
},
{
"travelerName":"王曉",
"travelerEmpNum":"1001",
"expenseAmountTotal":100,
"invoiceAmountTotal":100
},
{
"travelerName":"王曉",
"travelerEmpNum":"1001",
"expenseAmountTotal":306,
"invoiceAmountTotal":306
}
],
合并后的結(jié)果
[
{
expenseAmountTotal: 1381,
invoiceAmountTotal: 1381,
travelerEmpNum: "1001&1002",
travelerName: "王曉&陳辰",
travelers: " 王曉 | 1001, 陳辰 | 1002",
},
{
expenseAmountTotal: 406,
invoiceAmountTotal: 406,
travelerEmpNum: "1001",
travelerName: "王曉",
travelers: " 王曉 | 1001",
},
]
/ **
* 報(bào)賬單提交時(shí)費(fèi)用金額檢查
*/
async fetchcheckClaimAmount () {
this.claimAmountList = []
let formData = new FormData()
formData.append('claimId', this.editedOriginDetail.claimId)
let res = await this.checkClaimAmount(formData)
if (res.success) {
let list = res.rows
let result = list.reduce((items, item) => { // 將相同員工屬性合并
let findData= items.find(arrItem => item.travelerEmpNum === arrItem.travelerEmpNum)
if (findData) {
findData.invoiceAmountTotal += item.invoiceAmountTotal
findData.expenseAmountTotal += item.expenseAmountTotal
} else {
//不要直接在原來的對(duì)象修改,新建對(duì)象
let newItem = {
expenseAmountTotal: item.expenseAmountTotal,
invoiceAmountTotal: item.invoiceAmountTotal,
travelerEmpNum: item.travelerEmpNum,
travelerName: item.travelerName,
}
items.push(newItem )
}
return items
},[])
// 員工信息處理
result.forEach(item => {
if (item.expenseAmountTotal !== item.invoiceAmountTotal) { // 若發(fā)票合計(jì)與費(fèi)用合計(jì)進(jìn)入不同則展示在頁面
this.claimAmountList.push(item)
let travelers = ''
if (item.travelerEmpNum.indexOf('&') !== -1) { // 多個(gè)員工
let code = item.travelerEmpNum.split('&')
let name = item.travelerName.split('&')
code.forEach((el, i) => {
name.forEach((it, y) => {
if (i === y) {
travelers += ` ${it} | ${el},`
}
})
})
item['travelers'] = travelers.substring(0, travelers.length - 1)
} else { // 一個(gè)員工
item['travelers'] = ` ${item.travelerName} | ${item.travelerEmpNum}`
}
}
})
if (this.claimAmountList.length) {
this.checkVisible = true
} else {
this.checkClaimOk()
}
}
},