接口返回回来的数据是这样的格式:
[{ "payType": "06", "paymentDate": "2018-06-02 22:41:17", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年6月", "date": "06-02", "time": "22:41:17", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-06-02 22:41:17", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年6月", "date": "06-02", "time": "22:41:17", "price": "0.20", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-04-02 17:46:00", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年4月", "date": "04-02", "time": "17:46:00", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-04-02 17:46:00", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年4月", "date": "04-02", "time": "17:46:00", "price": "0.20", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-03-02 07:55:41", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年3月", "date": "03-02", "time": "07:55:41", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-03-02 07:55:41", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年3月", "date": "03-02", "time": "07:55:41", "price": "0.20", "resource": "第三方支付", "other": "其他" }]
需要对上方数据做处理的逻辑代码:
var formatData = { mainList: [] }; for (var i = 0; i < oCapHistoryinfo.length; i++) { var num = formatData.mainList.length; var date = oCapHistoryinfo[i].yearMonth; if (num > 0 && !!formatData.mainList[num - 1].date && formatData.mainList[num - 1].date == date) { formatData.mainList[num - 1].itemList.push(oCapHistoryinfo[i]); } else { var item = { date: date, itemList: [] }; item.itemList.push(oCapHistoryinfo[i]); formatData.mainList.push(item); } } console.log('arr', JSON.stringify(formatData))
处理后的代码:
{ "mainList": [{ "date": "2018年6月", "itemList": [{ "payType": "06", "paymentDate": "2018-06-02 22:41:17", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年6月", "date": "06-02", "time": "22:41:17", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-06-02 22:41:17", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年6月", "date": "06-02", "time": "22:41:17", "price": "0.20", "resource": "第三方支付", "other": "其他" }] }, { "date": "2018年4月", "itemList": [{ "payType": "06", "paymentDate": "2018-04-02 17:46:00", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年4月", "date": "04-02", "time": "17:46:00", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-04-02 17:46:00", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年4月", "date": "04-02", "time": "17:46:00", "price": "0.20", "resource": "第三方支付", "other": "其他" }] }, { "date": "2018年3月", "itemList": [{ "payType": "06", "paymentDate": "2018-03-02 07:55:41", "staffappprem": "99.80", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年3月", "date": "03-02", "time": "07:55:41", "price": "99.80", "resource": "第三方支付", "other": "其他" }, { "payType": "06", "paymentDate": "2018-03-02 07:55:41", "staffappprem": "0.20", "paymentDateCode": "99", "payTypeName": "第三方支付", "yearMonth": "2018年3月", "date": "03-02", "time": "07:55:41", "price": "0.20", "resource": "第三方支付", "other": "其他" }] }] }