// 这是后台数据var dataSource = [{"saturday": 2764,"project": "过店人次","thursday": 3389,"sum": 18958,"sunday": 2364,"tuesday": 2945,"wednesday": 3452,"friday": 1398,"monday": 2646},{"saturday": 17,"project": "环比上周","thursday": 73,"sum": 205,"sunday": 9,"tuesday": 48,"wednesday": 54,"friday": -39,"monday": 43},{"saturday": -36,"project": "对比今年均值","thursday": -21,"sum": -261,"sunday": -45,"tuesday": -32,"wednesday": -20,"friday": -68,"monday": -39},{"saturday": "296 (17 - 18点)","project": "过店峰值1","thursday": "326 (10 - 11点)","sum": 2348,"sunday": "297 (9 - 10点)","tuesday": "314 (17 - 18点)","wednesday": "352 (8 - 9点)","friday": "435 (8 - 9点)","monday": "328 (18 - 19点)"},{"saturday": "289 (8 - 9点)","project": "过店峰值2","thursday": "325 (9 - 10点)","sum": 2052,"sunday": "236 (8 - 9点)","tuesday": "284 (8 - 9点)","wednesday": "299 (10 - 11点)","friday": "298 (17 - 18点)","monday": "321 (17 - 18点)"},{"saturday": "285 (9 - 10点)","project": "过店峰值3","thursday": "298 (8 - 9点)","sum": 1866,"sunday": "219 (12 - 13点)","tuesday": "260 (9 - 10点)","wednesday": "297 (11 - 12点)","friday": "272 (9 - 10点)","monday": "235 (10 - 11点)"}]// 想要的data var newData = [// 周一就是每个对象里的monday// key可以自定义的英文唯一值{'日期': '周一','过店人次': 2646,'环比上周': 43,'对比今年均值': -39,'过店峰值1': "328 (18 - 19点)",'过店峰值2': "321 (17 - 18点)",'过店峰值3': "235 (10 - 11点)"},// 以此类推 周二tuesday 周二就是每个对象里面的tuesday的{'日期': '周二 ','过店人次': 2646,'环比上周': 43,'对比今年均值': -39,'过店峰值1': "328 (18 - 19点)",'过店峰值2': "321 (17 - 18点)",'过店峰值3': "235 (10 - 11点)"},//... 34 5 6 7 合计]// 函数
//方法1 let mapData = new Map([["monday", "周一"],["tuesday", "周二"],["wednesday", "周三"],["thursday", "周四"],["friday", "周五"],["saturday", "周六"],["sunday", "周日"]])let mapData1 = new Map([["周一", "1"],["周二", "2"],["周三", "3"],["周四", "4"],["周五", "5"],["周六", "6"],["周日", "7"],["合计", "8"],])function changeAir(params) {let temp = params.projectreturn temp}for (const key in dataSource[0]) {mapData.get(key) && newData.push({'日期':key})}dataSource.forEach((item,index)=>{let temp = changeAir(item)newData.map(i=>{i[temp] = item.projectif(item.project === temp){i[temp] = item[i['日期']]}})})newData.map(item=>{item.dataName = mapData.get(item['日期'])item.enData = item['日期']item.sort = mapData1.get(item.dataName)delete item['日期']})console.log(newData)
//方法2var daobulArr = ['过店人次', '环比上周', '对比今年均值', '过店峰值1', '过店峰值2', '过店峰值3']dataSource.map((item) => {if (item.project === '过店人次') {delete item.projectfor (let keys in item) {dataArrs.push({outTotal: item[keys],dataName: test(keys).dataName,enData: keys,sort: test(keys).sort})}} else {daobulArr.map((info) => {if (item.project === info) {for (let keys in item) {dataArrs.map((fun) => {fun[`${info}`] = item[fun.enData]})}}})}})function test(key) {let contObj = {dataName: '',sort: 0}switch (key) {case 'monday':contObj.dataName = '周一'contObj.sort = 1break;case 'tuesday':contObj.dataName = '周二'contObj.sort = 2break;case 'wednesday':contObj.dataName = '周三'contObj.sort = 3break;case 'thursday':contObj.dataName = '周四'contObj.sort = 4break;case 'friday':contObj.dataName = '周五'contObj.sort = 5break;case 'saturday':contObj.dataName = '周六'contObj.sort = 6break;case 'sunday':contObj.dataName = '周日'contObj.sort = 7break;case 'sum':contObj.dataName = '合计'contObj.sort = 8break;default:break;}return contObj}