a543462378 2022-06-16 16:16 采纳率: 87.9%
浏览 45
已结题

后端返回的数组,如何拆分

后端返回的数据,现在要把每一条listDetailed里面的数据和外面的合并。listDetailed里面有几条就生成几条新数据。
就是把下面的数据

{
                "shipFid":"船舶编号",
                "shipName":"船舶名称",
                "shipCompanyFid":"船公司编号",
                "shipCompanyName":"船公司名称",
                "shipTotal":10.2,
                "replacementFrequency":10,
                "washFrequency":11,
                "taskNumber":12,
                "listDetailed":[
                    {
                        "workStartTime":"2022-06-16",
                        "workEndTime":"2022-06-20",
                        "workPort":"装卸作业码头",
                        "workType":"主要货中",
                        "situation":"洗舱情况",
                    },
                    {
                        "workStartTime":"2022-06-16",
                        "workEndTime":"2022-06-20",
                        "workPort":"装卸作业码头1",
                        "workType":"主要货中1",
                        "situation":"洗舱情况1",
                    }
                ]
            },{
                "shipFid":"船舶编号",
                "shipName":"船舶名称",
                "shipCompanyFid":"船公司编号",
                "shipCompanyName":"船公司名称",
                "shipTotal":10.2,
                "replacementFrequency":10,
                "washFrequency":11,
                "taskNumber":12,
                "listDetailed":[
                    {
                        "workStartTime":"2022-06-16",
                        "workEndTime":"2022-06-20",
                        "workPort":"装卸作业码头",
                        "workType":"主要货中",
                        "situation":"洗舱情况",
                    },
                    {
                        "workStartTime":"2022-06-16",
                        "workEndTime":"2022-06-20",
                        "workPort":"装卸作业码头1",
                        "workType":"主要货中1",
                        "situation":"洗舱情况1",
                    },
                    {
                        "workStartTime":"2022-06-16",
                        "workEndTime":"2022-06-20",
                        "workPort":"装卸作业码头1",
                        "workType":"主要货中1",
                        "situation":"洗舱情况1",
                    }
                ]
            },

变成这种格式

{
    "shipFid":"船舶编号",
    "shipName":"船舶名称",
    "shipCompanyFid":"船公司编号",
    "shipCompanyName":"船公司名称",
    "shipTotal":10.2,
    "replacementFrequency":10,
    "washFrequency":11,
    "taskNumber":12,
    "workStartTime":"2022-06-16",
    "workEndTime":"2022-06-20",
    "workPort":"装卸作业码头",
    "workType":"主要货中",
    "situation":"洗舱情况",
},{
    "shipFid":"船舶编号",
    "shipName":"船舶名称",
    "shipCompanyFid":"船公司编号",
    "shipCompanyName":"船公司名称",
    "shipTotal":10.2,
    "replacementFrequency":10,
    "washFrequency":11,
    "taskNumber":12,
    "workStartTime":"2022-06-16",
    "workEndTime":"2022-06-20",
    "workPort":"装卸作业码头",
    "workType":"主要货中",
    "situation":"洗舱情况",
},{
    "shipFid":"船舶编号",
    "shipName":"船舶名称",
    "shipCompanyFid":"船公司编号",
    "shipCompanyName":"船公司名称",
    "shipTotal":10.2,
    "replacementFrequency":10,
    "washFrequency":11,
    "taskNumber":12,
    "workStartTime":"2022-06-16",
    "workEndTime":"2022-06-20",
    "workPort":"装卸作业码头",
    "workType":"主要货中",
    "situation":"洗舱情况",
},{
    "shipFid":"船舶编号",
    "shipName":"船舶名称",
    "shipCompanyFid":"船公司编号",
    "shipCompanyName":"船公司名称",
    "shipTotal":10.2,
    "replacementFrequency":10,
    "washFrequency":11,
    "taskNumber":12,
    "workStartTime":"2022-06-16",
    "workEndTime":"2022-06-20",
    "workPort":"装卸作业码头",
    "workType":"主要货中",
    "situation":"洗舱情况",
},

  • 写回答

1条回答 默认 最新

  • 崽崽的谷雨 2022-06-16 16:58
    关注

    数据 层数 会 超过 2层吗?
    循环遍历 合并 对象

    
    let data = [
            {
                "shipFid": "船舶编号",
                "shipName": "船舶名称",
                "shipCompanyFid": "船公司编号",
                "shipCompanyName": "船公司名称",
                "shipTotal": 10.2,
                "replacementFrequency": 10,
                "washFrequency": 11,
                "taskNumber": 12,
                "listDetailed": [
                    {
                        "workStartTime": "2022-06-16",
                        "workEndTime": "2022-06-20",
                        "workPort": "装卸作业码头",
                        "workType": "主要货中",
                        "situation": "洗舱情况",
                    },
                    {
                        "workStartTime": "2022-06-16",
                        "workEndTime": "2022-06-20",
                        "workPort": "装卸作业码头1",
                        "workType": "主要货中1",
                        "situation": "洗舱情况1",
                    }
                ]
            }, {
                "shipFid": "船舶编号",
                "shipName": "船舶名称",
                "shipCompanyFid": "船公司编号",
                "shipCompanyName": "船公司名称",
                "shipTotal": 10.2,
                "replacementFrequency": 10,
                "washFrequency": 11,
                "taskNumber": 12,
                "listDetailed": [
                    {
                        "workStartTime": "2022-06-16",
                        "workEndTime": "2022-06-20",
                        "workPort": "装卸作业码头",
                        "workType": "主要货中",
                        "situation": "洗舱情况",
                    },
                    {
                        "workStartTime": "2022-06-16",
                        "workEndTime": "2022-06-20",
                        "workPort": "装卸作业码头1",
                        "workType": "主要货中1",
                        "situation": "洗舱情况1",
                    },
                    {
                        "workStartTime": "2022-06-16",
                        "workEndTime": "2022-06-20",
                        "workPort": "装卸作业码头1",
                        "workType": "主要货中1",
                        "situation": "洗舱情况1",
                    }
                ]
            },
        ];
        console.log(mergeData(data))
    
        function mergeData(data) {
            let arr = [];
            data.map((item) => {
                if (item.listDetailed) {
                    item.listDetailed.map((item1) => {
                        //合并 对象
                        let obj = { ...item, ...item1 };
                        //删除 listDetailed
                        delete obj.listDetailed;
                        // push到新数组中
                        arr.push(obj);
                    })
                }
            });
            return arr;
        }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

    报告相同问题?

    问题事件

    • 系统已结题 6月24日
    • 已采纳回答 6月16日
    • 创建了问题 6月16日

    悬赏问题

    • ¥15 Tpad api账户 api口令
    • ¥30 ppt进度条制作,vba语言
    • ¥15 stc12c5a60s2单片机测光敏ADC
    • ¥15 生信simpleaffy包下载
    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能