支付报表 把几个对象推平放在一个对象中,_.assign
let data = [ { appName: "APP", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "01", tradeWay: "" }, { appName: "APP", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "02", tradeWay: "" }, { appName: "APP", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "03", tradeWay: "" }, { appName: "APP", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "04", tradeWay: "" }, { appName: "自助机", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "01", tradeWay: "" }, { appName: "自助机", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "02", tradeWay: "" }, { appName: "自助机", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "03", tradeWay: "" }, { appName: "自助机", payCounts: 9, refundAmount: 0.1, refundCounts: 1, totalAmount: 433.32, tradeSubject: "04", tradeWay: "" }, ] let groupData = _.groupBy(data, 'appName') console.log(_.map(groupData, item => { return _.assign({}, ..._.map(item, val => { let obj = {} _.forEach(val, (value, key) => { obj[key + val.tradeSubject] = value }) return obj })) }))支付首页
manager 1234qwer 0: {payTime: "1597334400000", receiptAmount: "2.1", tradeSubject: "01"} 1: {payTime: "", receiptAmount: "", tradeSubject: "02"} 2: {payTime: "", receiptAmount: "", tradeSubject: "03"} 3: {payTime: "", receiptAmount: "", tradeSubject: "04"} 4: {payTime: "", receiptAmount: "", tradeSubject: "05"} 5: {payTime: "", receiptAmount: "", tradeSubject: "06"} 6: {payTime: "", receiptAmount: "", tradeSubject: "01"} 7: {payTime: "", receiptAmount: "", tradeSubject: "02"} 8: {payTime: "", receiptAmount: "", tradeSubject: "03"} 9: {payTime: "", receiptAmount: "", tradeSubject: "04"} … … … … … … 36: {payTime: "", receiptAmount: "", tradeSubject: "06"} let aaaa = _.groupBy(data, 'tradeSubject') 01: (6) [{…}, {…}, {…}, {…}, {…}, {…}] 0: {payTime: "1597334400000", receiptAmount: "2.1", tradeSubject: "01"} 1: {payTime: "", receiptAmount: "", tradeSubject: ""} 2: {payTime: "", receiptAmount: "", tradeSubject: ""} 3: {payTime: "", receiptAmount: "", tradeSubject: ""} 4: {payTime: "", receiptAmount: "", tradeSubject: ""} 5: {payTime: "", receiptAmount: "", tradeSubject: ""} 02: (6) [{…}, {…}, {…}, {…}, {…}, {…}] 03: (5) [{…}, {…}, {…}, {…}, {…}] 04: (6) [{…}, {…}, {…}, {…}, {…}, {…}] 05: (5) [{…}, {…}, {…}, {…}, {…}] 06: (5) [{…}, {…}, {…}, {…}, {…}] let bbbb = _.get(aaaa, '01', []) 0: {payTime: "1597334400000", receiptAmount: "2.1", tradeSubject: "01"} 0: payTime: 1597334400000 receiptAmount: 2.1 tradeSubject: "01" 1: {payTime: "", receiptAmount: "", tradeSubject: ""} 2: {payTime: "", receiptAmount: "", tradeSubject: ""} 3: {payTime: "", receiptAmount: "", tradeSubject: ""} 4: {payTime: "", receiptAmount: "", tradeSubject: ""} 5: {payTime: "", receiptAmount: "", tradeSubject: ""} let ccccc = _.map([ { 'key': '01', name: '挂号', witdth: '30' }, { 'key': '02', name: '取药', witdth: '30' }, { 'key': '03', name: '检查', witdth: '30' }, { 'key': '04', name: '检验', witdth: '30' }, { 'key': '05', name: '住院', witdth: '30' }, { 'key': '06', name: '体检', witdth: '30' } ], ({ key, name, witdth: barMaxWidth }) => { if (!!_.get(aaaa, key)) { return { name, type: 'bar', barMaxWidth, data: _.map(_.get(aaaa, key, []), 'receiptAmount') } } }) 0: {__ob__: Observer} 0: barMaxWidth: "30" data: Array(6) data: Array(6) 0: 2.1 1: 1.1 2: 1 3: 1 4: 3.1 5: 2.1 name: "挂号" type: "bar" 1: {__ob__: Observer} 2: {__ob__: Observer} 3: {__ob__: Observer} 4: {__ob__: Observer} 5: {__ob__: Observer} ```