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


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

    [{
        "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": "其他"
            }]
        }]
    }
  • 相关阅读:
    【More Effective C++ 条款4】非必要不提供default constructor
    【More Effective C++ 条款3】最好不要以多态方式处理数组
    【C++】Debug模式和Release模式的区别
    【More Effective C++ 条款2】最好使用C++转型操作符
    【More Effective C++ 条款1】仔细区别pointers和references
    程序崩溃时的堆栈捕捉
    快速排序--来自维基百科
    十六进制转为字符串
    linux那些事
    svnsync备份
  • 原文地址:https://www.cnblogs.com/suntao666/p/9253720.html
Copyright © 2020-2023  润新知