js如何扁平化复杂的数据结构,来个大佬解决哈!求!

需求1:将formData字段遍历插进外层,相同字段都以要插的表单字段为主,
需求2:将innerFormData字段遍历插进内层,相同字段都以要插的表单字段为主,
需求3:将原始orginObj对象下的deliveryNoteDetailVos改成stockRegisterDetailList,
需求4:将原始orginObj对象下stockRegisterDetailList里面的sourceCodeList提取出来,相同字段,以要提取出来的字段覆盖为主。
参考理想数据: resultObj,多谢大佬!!!

外层表单数据:

const formData = {
      createTime: '2022-05-01',
      createdepartmentCode: '22154',
      status: 1
}

内层表单数据:

 const innerFormData = {
      createUer: '张铭清',
      createdepartmentCode: '101210',
}

原始数据: orginObj

    const orginObj = [
      {
        createTime: '',
        createdepartmentCode: '101112',
        id: '1001',
        status: null,
        deliveryNoteDetailVos: [
          {
            createUer: 'null',
            createdepartmentCode: 'null',
            id: 'yQvmraqdqrvmK2ml9B0',
            productName: '医用胶带',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            sourceCodeList: [
              {
                code: 'l9bX5S5GLsYo9ZZfh6u',
                rejectionProcessType: 1,
                rejectionDescription: '222222222222',
                checkStatus: 2,
              },
              {
                code: 'UhTH3ftpUA7jAAfau8U',
                rejectionProcessType: null,
                rejectionDescription: null,
                checkStatus: 1,
              }
            ],
          },
          {
            createUer: 'null',
            createdepartmentCode: 'null',
            id: 'yQvmax2vmK2ml9B0',
            productName: '剪刀',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            sourceCodeList: [
              {
                code: 'xxadswe545112',
                rejectionProcessType: 1,
                rejectionDescription: '1111111',
                checkStatus: 2,
              },
              {
                code: '2a4414xqw',
                rejectionProcessType: 1,
                rejectionDescription: '12233333',
                checkStatus: 2,
              }
            ],
          }
        ],
      },
      {
        createTime: '',
        createdepartmentCode: '101112',
        id: '1002',
        status: 1,
        deliveryNoteDetailVos: [
          {
            id: 'yQvmraqdqrvmK2mx1',
            productName: '化学仪器',
            batch: '2441',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            sourceCodeList: [
              {
                code: 'x545451a1213',
                rejectionProcessType: 1,
                rejectionDescription: '333333333',
                checkStatus: 1,
              },
              {
                code: 'UhTH3ftpUAasdau8U',
                rejectionProcessType: null,
                rejectionDescription: null,
                checkStatus: 1,
              },
              {
                code: 'UhTH3ftpUAasd113',
                rejectionProcessType: null,
                rejectionDescription: null,
                checkStatus: 2,
              }
            ],
          },
          {
            id: 'xxymraqdvm121ml9',
            productName: '纱布',
            batch: '1510455',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            sourceCodeList: [
              {
                id: 'x12dswe545112',
                rejectionProcessType: 1,
                rejectionDescription: '6666666666',
                checkStatus: 2,
              },
            ],
          }
        ],
      },
    ]

理想数据: resultObj

  const resultObj = [
      {
        id: '1001',
        createTime: '2022-05-01',
        createdepartmentCode: '22154',
        status: 1,
        stockRegisterDetailList: [
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmraqdqrvmK2ml9B0',
            productName: '医用胶带',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'l9bX5S5GLsYo9ZZfh6u',
            rejectionProcessType: 1,
            rejectionDescription: '222222222222',
            checkStatus: 2,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmraqdqrvmK2ml9B0',
            productName: '医用胶带',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'UhTH3ftpUA7jAAfau8U',
            rejectionProcessType: null,
            rejectionDescription: null,
            checkStatus: 1,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmax2vmK2ml9B0',
            productName: '剪刀',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'xxadswe545112',
            rejectionProcessType: 1,
            rejectionDescription: '1111111',
            checkStatus: 2,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmax2vmK2ml9B0',
            productName: '剪刀',
            batch: '6784563',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: '2a4414xqw',
            rejectionProcessType: 1,
            rejectionDescription: '12233333',
            checkStatus: 2,
          }
        ],
      },
      {
        createTime: '2022-05-01',
        createdepartmentCode: '22154',
        status: 1,
        id: '1002',
        stockRegisterDetailList: [
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmraqdqrvmK2mx1',
            productName: '化学仪器',
            batch: '2441',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'x545451a1213',
            rejectionProcessType: 1,
            rejectionDescription: '333333333',
            checkStatus: 1,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmraqdqrvmK2mx1',
            productName: '化学仪器',
            batch: '2441',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'UhTH3ftpUAasdau8U',
            rejectionProcessType: null,
            rejectionDescription: null,
            checkStatus: 1,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'yQvmraqdqrvmK2mx1',
            productName: '化学仪器',
            batch: '2441',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'UhTH3ftpUAasd113',
            rejectionProcessType: null,
            rejectionDescription: null,
            checkStatus: 2,
          },
          {
            createUer: '张铭清',
            createdepartmentCode: '101210',
            id: 'xxymraqdvm121ml9',
            productName: '纱布',
            batch: '1510455',
            rejectionProcessType: null,
            rejectionProcessTypeName: null,
            rejectionDescription: null,
            num: 2,
            code: 'x12dswe545112',
            rejectionProcessType: 1,
            rejectionDescription: '6666666666',
            checkStatus: 2,
          }
        ],
      },
    ]
阅读 2.5k
3 个回答

这里的实现是根据你的需求描述去实现的。但好像和你给出的示例结果不同。
比如resultObj[0].stockRegisterDetailList[0].rejectionProcessType1,但是你的示例结果是null,但这个好像和你的需求4的描述不符。

const resultObj = orginObj.map(
    ({ deliveryNoteDetailVos, ...outer }) => ({
        ...outer,
        ...formData,
        stockRegisterDetailList: deliveryNoteDetailVos.flatMap(
            ({ sourceCodeList, ...inner }) =>
                sourceCodeList.map(
                    sourceCode => ({
                        ...inner,
                        ...sourceCode,
                        ...innerFormData,
                    }),
                ),
        ),
    }),
);

所有的数据扁平化,其实是一种数据格式转换,这需要有整体的策略与规则(体现在你的具体要求中)。因为这和后面如何使用转换后的数据是密切相关的,所以需要和后面如何使用数据向关联,从现在你给的信息来说还不明确,甚至有冲突。

这个主要还是要自己明确一些规则,再谈实现。

已参与了 SegmentFault 思否社区 10 周年「问答」打卡 ,欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题