//?有行合并頭不建議使用該方法
?????getYsXjSum()?{
??????var?tableList?=?this.tableList;
??????let?res=?[];
??????let?len?=?tableList.length-1
??????//每4行再推入一行
??????for(let?i=0;i<len;i=i+4){
????????if(i+4<len){
???????????res.push(tableList.slice(i,i+4));
?????????}else{
??????????res.push(tableList.slice(i));
}
}
??????//推入后打印效果
??????console.log('the?old?array?is',res);
??????//?每小列合計(jì)
??????res.forEach(?(item)=>{
????????let?total=?{}
?????????for?(let?key?of?Object.keys(item[0]))?{
?????????//total[key]?=?item.reduce((Sum,?ele)?=>?{
?????????//console.log('ele----------',ele,?'key-------------',ele[key])
?????????//return?Sum?+?ele[key]
??????????total[key]?=?item.reduce((Sum,?ele)?=>{
????????????console.log('ele----------',ele,?'key-------------',ele[key])
????????????//?parseFloat(ele[key])?!==?NaN
????????????let?num?=?0
????????????if?(!isNaN(parseFloat(ele[key])))?{num?=?parseFloat(ele[key])}
??????????????return?Sum?+?num
});
??????//??});
??????//???total[key]?=?item.reduce((Sum,?ele)?=>?(Sum?+(ele[key]?||?0)),?0)
}
??????//??item.push(total)
??????//?})
???????let?result?=?[].concat(...res);
???????this.tableList=result
???????console.log('result:',result)
})
}
簡(jiǎn)寫不為對(duì)象:
total[key]
= item.reduce((Sum, ele) => {
?? let num = 0
?? if (parserFloat(ele[key]) !== NaN) {num =parserFloat(ele[key])}
?? return Sum + num
? }, 0);