前端处理返回数据为数组对象且对象嵌套数组并重名的数据,合并名称并叠加数据
var newList= [ ] ;
var table= { } ;
var dataObj= { } ;
var finalList= [ ] ;
var tableData= [
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '20' , tacheName : '奔驰' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '30' , tacheName : '宝马' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '40' , tacheName : '宝马' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '50' , tacheName : '帕萨特' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '60' , tacheName : '法拉利' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '70' , tacheName : '帕萨特' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '80' , tacheName : '法拉利' } ] } ,
{ brName : '营业部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '90' , tacheName : '奔驰' } ] } ,
{ brName : '销售部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '10' , tacheName : '奔驰' } ] } ,
{ brName : '销售部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '50' , tacheName : '奔驰' } ] } ,
{ brName : '销售部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '70' , tacheName : '帕萨特' } ] } ,
{ brName : '销售部' , dateStr : '2023-11-23' , tacheArr : [ { dealCnt : '20' , tacheName : '帕萨特' } ] } ,
] ;
for ( let i in tableData) {
brName= tableData[ i] . brName;
dateStr= tableData[ i] . dateStr;
tacheArr= tableData[ i] . tacheArr;
if ( table[ brName] == null ) {
if ( tacheArr[ 0 ] . tacheName== '奔驰' ) {
table[ brName] = { 'name' : brName, 'dateStr' : dateStr, 'val1' : Number ( tacheArr[ 0 ] . dealCnt) , 'val2' : 0 , 'val3' : 0 , 'val4' : 0 , 'val5' : 0 , }
} else if ( tacheArr[ 0 ] . tacheName== '宝马' ) {
table[ brName] = { 'name' : brName, 'dateStr' : dateStr, 'val1' : 0 , 'val2' : Number ( tacheArr[ 0 ] . dealCnt) , 'val3' : 0 , 'val4' : 0 , 'val5' : 0 , }
} else if ( tacheArr[ 0 ] . tacheName== '法拉利' ) {
table[ brName] = { 'name' : brName, 'dateStr' : dateStr, 'val1' : 0 , 'val2' : 0 , 'val3' : Number ( tacheArr[ 0 ] . dealCnt) , 'val4' : 0 , 'val5' : 0 , }
} else if ( tacheArr[ 0 ] . tacheName== '布加迪' ) {
table[ brName] = { 'name' : brName, 'dateStr' : dateStr, 'val1' : 0 , 'val2' : 0 , 'val3' : 0 , 'val4' : Number ( tacheArr[ 0 ] . dealCnt) , 'val5' : 0 , }
} else if ( tacheArr[ 0 ] . tacheName== '帕萨特' ) {
table[ brName] = { 'name' : brName, 'dateStr' : dateStr, 'val1' : 0 , 'val2' : 0 , 'val3' : 0 , 'val4' : 0 , 'val5' : Number ( tacheArr[ 0 ] . dealCnt) , }
}
} else {
if ( tacheArr[ 0 ] . tacheName== '奔驰' ) {
table[ brName] . val1+= Number ( tacheArr[ 0 ] . dealCnt)
} else if ( tacheArr[ 0 ] . tacheName== '宝马' ) {
table[ brName] . val2+= Number ( tacheArr[ 0 ] . dealCnt)
} else if ( tacheArr[ 0 ] . tacheName== '法拉利' ) {
table[ brName] . val3+= Number ( tacheArr[ 0 ] . dealCnt)
} else if ( tacheArr[ 0 ] . tacheName== '布加迪' ) {
table[ brName] . val4+= Number ( tacheArr[ 0 ] . dealCnt)
} else if ( tacheArr[ 0 ] . tacheName== '帕萨特' ) {
table[ brName] . val5+= Number ( tacheArr[ 0 ] . dealCnt)
}
}
}
for ( key in table) {
newList. push ( table[ key] )
}
for ( let k= 0 ; k< newList. length; k++ ) {
dataObj= { 'brName' : newList[ k] . name, 'dateStr' : newList[ k] . dateStr, 'val1' : newList[ k] . val1, 'val2' : newList[ k] . val2, 'val3' : newList[ k] . val3, 'val4' : newList[ k] . val4, 'val5' : newList[ k] . val5, }
finalList. push ( dataObj)
}
console. log ( finalList)
这里可以看到我们输出的结果,我们已经将重名的数据合并并且相同的数据叠加合并