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

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

    [{
        "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": "其他"
            }]
        }]
    }
  • 相关阅读:
    李洪强经典面试题51-KVO-KVC
    李洪强经典面试题50-内存管理
    李洪强经典面试题49-Objective-C
    李洪强经典面试题48-C语言
    李洪强经典面试题47--UNIX常用命令
    李洪强经典面试题46-版本控制
    绿麻雀网贷系统通用注入分析
    跟着百度学PHP[5]函数篇2-PHP中的特殊形式的函数
    报错注入分析之(count()、rand()、group by)分析,被大佬称为floor报错注入
    跟着百度学PHP[5]函数篇1-参数
  • 原文地址:https://www.cnblogs.com/suntao666/p/9253720.html
Copyright © 2011-2022 走看看