接口返回回来的数据是一个数组,里面有同年同月,需要把同年同月的合并到一个数组里面并且把相同年月的数据放到子数组中

接口返回回来的数据是这样的格式:

[{
    "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": "其他"
        }]
    }]
}

 

posted on 2018-07-02 14:41  道法自然-法力无边  阅读(236)  评论(0编辑  收藏  举报

导航