a543462378 2022-06-16 16:16 采纳率: 82%
浏览 46
已结题

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

后端返回的数据,现在要把每一条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 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错