JS 将对象数组子项合并到父项

数据格式 

let arr = [{
            "parentNodeName": "车辆1",
            "parentNodeId": 1,
            "nodeName": "转向控制",
            "nodeId": 19,
            "baseLibraryId": "1663791170918150145",
            "childNodeList": [
                {
                    "id": 32,
                    "tmDfmeaId": "1663742185075671041",
                    "itemName": "方向盘",
                    "parentId": 19,
                    "nodeType": "零部件",
                    "nodeLevel": 3
                },
                {
                    "id": 3184,
                    "deleteFlag": 0,
                    "tmDfmeaId": "1663742185075671041",
                    "itemName": "方向电控",
                    "parentId": 19,
                    "nodeType": "零部件",
                    "nodeLevel": 3
                }
            ],
            "functionList": [
                {
                    "parentFunctionList": [
                        {
                            "nodeId": 15,
                            "nodeName": "方向电控",
                            "baseLibraryId": "1664533904775823361",
                            "sort": 9,
                            "rcList": [
                                {
                                    "id": 16,
                                    "deleteFlag": 0,
                                    "tmDfmeaId": "1663742185075671041",
                                    "itemName": "特性1",
                                    "parentId": 15,
                                    "nodeType": "要求特性",
                                    "nodeLevel": 3,
                                    "ancestors": "-1,1,15",
                                    "sort": null,
                                    "baseLibraryId": "1665622349820837889",
                                }
                            ]
                        },
                        {
                            "nodeId": 17,
                            "nodeName": "平稳",
                            "baseLibraryId": "1664536344296529921",
                            "sort": null,
                            "rcList": [
                                {
                                    "id": 18,
                                    "deleteFlag": 0,
                                    "tmDfmeaId": "1663742185075671041",
                                    "itemName": "大于100",
                                    "parentId": 17,
                                    "nodeType": "要求特性",
                                    "nodeLevel": 3,
                                    "ancestors": "-1,1,17"
                                }
                            ]
                        }
                    ],
                    "currentFunction": {
                        "nodeId": 20,
                        "nodeName": "安全行驶",
                        "baseLibraryId": "1664536400391151618",
                        "sort": null,
                        "rcList": [
                            {
                                "id": 21,
                                "tmDfmeaId": "1663742185075671041",
                                "itemName": "要求1",
                                "parentId": 20,
                                "nodeType": "要求特性",
                                "nodeLevel": 4
                            },
                            {
                                "id": 34,
                                "tmDfmeaId": "1663742185075671041",
                                "itemName": "要求2",
                                "parentId": 20,
                                "nodeType": "要求特性",
                                "nodeLevel": 4
                            }
                        ]
                    },
                    "lapseAnalyseOneList": [
                        {
                            "loseScoreList": [
                                {
                                    "nodeId": 22,
                                    "nodeName": "不舒服",
                                    "val": 5
                                },
                                {
                                    "nodeId": 3243,
                                    "nodeName": "方向偏移",
                                    "val": null
                                }
                            ],
                            "loseVal": 5,
                            "currentLoseNodeId": 23,
                            "currentLoseNodeName": "抖动",
                            "currentBaseLibraryId": "1664536782043455489",
                            "lapseAnalyseTowList": [
                                {
                                    "nodeId": 3188,
                                    "tmDfmeaId": null,
                                    "nodeName": "时速低于180",
                                    "baseLibraryId": "1664568460086378497",
                                    "childLosePcList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1",
                                            "tmBaseId": "2",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "提高人员的专业技术1",
                                            "measureName": "提高人员的专业技术"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "2",
                                            "tmBaseId": "4",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "工艺改进"
                                        }
                                    ],
                                    "childLosePcVal": 3,
                                    "childLoseDcList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1",
                                            "tmBaseId": "3",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "客户容易感知",
                                            "measureName": "客户容易感知",
                                            "val": 2,
                                            "type": "风险分析",
                                            "dcType": "FC",
                                            "showFlag": ""
                                        }
                                    ],
                                    "childLoseDcVal": 2,
                                    "riskAp": "L",
                                    "precautionList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667061491058356226",
                                            "tmBaseId": "3",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "温度控制1",
                                            "measureName": "温度控制",
                                            "val": 7,
                                            "type": "优化",
                                            "dcType": null,
                                            "showFlag": "否"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667062432369225730",
                                            "tmBaseId": "1667048322541826049",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "专业改进1",
                                            "measureName": "专业改进2",
                                            "showFlag": "否"
                                        }
                                    ],
                                    "detectionMeasureList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667101920065912834",
                                            "tmBaseId": "1",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "中空板黑屏",
                                            "showFlag": "否"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "2",
                                            "tmBaseId": "2",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "参数需要调整",
                                            "measureName": "参数需要调整",
                                            "val": 3,
                                            "type": "优化",
                                            "dcType": "FC",
                                            "showFlag": ""
                                        }
                                    ],
                                    "optimize": {
                                        "id": "1",
                                        "parentTreeId": 23,
                                        "tmTreeId": 3188,
                                        "leader": "admin",
                                        "leaderId": "admin",
                                        "planCompleteDate": "2023-06-02 08:44:43",
                                        "realityCompleteDate": "2023-06-07 08:44:48",
                                        "feVal": 5
                                    },
                                    "evidenceBasedMeasureList": [

                                    ],
                                    "optimizeFrequency": 7,
                                    "optimizeDetectability": null,
                                    "optimizeAp": null
                                },
                                {
                                    "nodeId": 3189,
                                    "tmDfmeaId": null,
                                    "nodeName": "时速低于160",
                                    "baseLibraryId": "1664537921791041537",
                                    "childLosePcList": null,
                                    "childLosePcVal": null,
                                    "childLoseDcList": null,
                                    "childLoseDcVal": null,
                                    "riskAp": null,
                                    "precautionList": null,
                                    "detectionMeasureList": null,
                                    "optimize": {
                                        "id": "1667102480089382913",
                                        "version": 1,
                                        "deleteFlag": 0
                                    },
                                    "evidenceBasedMeasureList": null,
                                    "optimizeFrequency": null,
                                    "optimizeDetectability": null,
                                    "optimizeAp": null
                                },
                                {
                                    "nodeId": 3191,
                                    "tmDfmeaId": null,
                                    "nodeName": "老化",
                                    "baseLibraryId": "1666028294438768641"
                                }
                            ]
                        },
                        {
                            "loseScoreList": [
                                {
                                    "nodeId": 24,
                                    "nodeName": "方向偏移",
                                    "val": null
                                }
                            ],
                            "loseVal": null,
                            "currentLoseNodeId": 3106,
                            "currentLoseNodeName": "有异响",
                            "currentBaseLibraryId": "1664538404198916097",
                            "lapseAnalyseTowList": [
                                {
                                    "nodeId": 3188,
                                    "tmDfmeaId": null,
                                    "nodeName": "时速低于180",
                                    "baseLibraryId": "1664568460086378497",
                                    "childLosePcList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1",
                                            "tmBaseId": "2",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "提高人员的专业技术1",
                                            "measureName": "提高人员的专业技术"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "2",
                                            "tmBaseId": "4",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "工艺改进",
                                            "measureName": "工艺改进"
                                        }
                                    ],
                                    "childLosePcVal": 3,
                                    "childLoseDcList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1",
                                            "tmBaseId": "3",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "客户容易感知",
                                            "measureName": "客户容易感知"
                                        }
                                    ],
                                    "childLoseDcVal": 2,
                                    "riskAp": null,
                                    "precautionList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667061491058356226",
                                            "tmBaseId": "3",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "温度控制1"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667062432369225730",
                                            "tmBaseId": "1667048322541826049",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "专业改进1",
                                            "measureName": "专业改进2"
                                        }
                                    ],
                                    "detectionMeasureList": [
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "1667101920065912834",
                                            "tmBaseId": "1",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "中空板黑屏"
                                        },
                                        {
                                            "tmDfmeaId": "1663742185075671041",
                                            "dcPcId": "2",
                                            "tmBaseId": "2",
                                            "parentTreeId": 23,
                                            "tmTreeId": 3188,
                                            "name": "参数需要调整",
                                            "measureName": "参数需要调整"
                                        }
                                    ],
                                    "optimize": {
                                        "id": "1",
                                        "version": null,
                                        "deleteFlag": 0,
                                        "state": null,
                                        "parentTreeId": 23,
                                        "tmTreeId": 3188,
                                        "leader": "admin",
                                        "leaderId": "admin",
                                        "planCompleteDate": "2023-06-02 08:44:43",
                                        "realityCompleteDate": "2023-06-07 08:44:48",
                                        "feVal": 5
                                    },
                                    "evidenceBasedMeasureList": [

                                    ],
                                    "optimizeFrequency": 7,
                                    "optimizeDetectability": null,
                                    "optimizeAp": null
                                },
                                {
                                    "nodeId": 3189,
                                    "tmDfmeaId": null,
                                    "nodeName": "时速低于160",
                                    "baseLibraryId": "1664537921791041537",
                                    "childLosePcList": null,
                                    "childLosePcVal": null,
                                    "childLoseDcList": null,
                                    "childLoseDcVal": null,
                                    "riskAp": null,
                                    "precautionList": null,
                                    "detectionMeasureList": null,
                                    "optimize": {
                                        "id": "1667102480089382913",
                                        "version": 1,
                                        "deleteFlag": 0
                                    },
                                    "evidenceBasedMeasureList": null,
                                    "optimizeFrequency": null,
                                    "optimizeDetectability": null,
                                    "optimizeAp": null
                                }
                            ]
                        }
                    ]
                },
                {
                    "parentFunctionList": [

                    ],
                    "currentFunction": {
                        "nodeId": 3182,
                        "nodeName": "安全",
                        "baseLibraryId": "1666022393384390657",
                        "sort": null,
                        "rcList": [
                            {
                                "id": 3183,
                                "deleteFlag": 0,
                                "tmDfmeaId": "1663742185075671041",
                                "itemName": "粘稠2",
                                "parentId": 3182,
                                "nodeType": "要求特性",
                                "nodeLevel": 4,
                                "ancestors": "-1,1,19,3182",
                                "baseLibraryId": "9",
                            }
                        ]
                    },
                    "lapseAnalyseOneList": [

                    ]
                }
            ]
        }]
const ret = arr.flatMap(({ functionList, ...other }) => {
  return functionList.map(item => {
    return { functionList: [item], ...item, ...other }
    })
  })
const ret1 = ret.flatMap(({ lapseAnalyseOneList, ...other }) => {
  return lapseAnalyseOneList.map(item => {
    return { lapseAnalyseOneList: [item], ...item, ...other }
    })
})
const ret2 = ret1.flatMap(({ lapseAnalyseTowList, ...other }) => {
  return lapseAnalyseTowList.map(item => {
     return { lapseAnalyseTowList: [item], ...item, ...other }
     })
})
console.log(ret2)

你可能感兴趣的:(javascript,es6)