js 如何遍历数据中心,根据某个参数去调接口组装数据。

例如:
1.接口ajaxList获取数据dataList

const dataList = [
  {
    code: 1001,
    name: 'zs',
    detailList: [
      { cId: 'c-10001', uid: 'GYS-00901', name: 'aaa' },
      { cId: 'c-10002', uid: 'GYS-00902', name: 'bbb' },
      { cId: 'c-10003', uid: 'GYS-00903', name: 'ccc' },
    ],
  },
  {
    code: 1002,
    name: 'lisi',
    detailList: [
      { cId: 'c-10004', uid: 'GYS-00904', name: 'eee' },
      { cId: 'c-10005', uid: 'GYS-00905', name: 'ttt' },
      { cId: 'c-10006', uid: 'GYS-00906', name: 'qqq' },
    ],
  },
  {
    code: 1003,
    name: 'wangwu',
    detailList: [
      { cId: 'c-10007', uid: 'GYS-00907', name: 'mm' },
      { cId: 'c-10008', uid: 'GYS-00908', name: 'nn' },
    ],
  },
]

2.根据dataList里面的 detailList的每条数据 { cId: 'c-10001', uid: 'GYS-00901', name: 'aaa' }, 的参数params({ cId: 'c-10001', uid: 'GYS-00901'}) 去调用检查思否通过的接口 ajaxBatchValidate(params) 获得数据 ValidateData

const ValidateData = [
  { reasonList: '说明书正确', status: true },
  { reasonList: '缺少资质说明', status: false },
]

3.把2中获取的结果 插入到 dataList里detailList每条验证结果中

期望数据结构:

const dataList = [
  {
    code: 1001,
    name: 'zs',
    detailList: [
      {
        cId: 'c-10001',
        uid: 'GYS-00901',
        name: 'aaa',
        ValidateLsit: [
          { reasonList: '说明书正确', status: true },
          { reasonList: '缺少资质说明', status: false },
        ],
      },
      {
        cId: 'c-10002',
        uid: 'GYS-00902',
        name: 'bbb',
        ValidateLsit: [
          { reasonList: 'xxxx', status: true },
          { reasonList: 'xxxx', status: false },
        ],
      },
      {
        cId: 'c-10003',
        uid: 'GYS-00903',
        name: 'ccc',
        ValidateLsit: [
          { reasonList: 'xxxx', status: true },
          { reasonList: 'xxxx', status: false },
        ],
      },
    ],
  },
  {
    code: 1002,
    name: 'lisi',
    detailList: [
      {
        cId: 'c-10004',
        uid: 'GYS-00904',
        name: 'eee',
        ValidateLsit: [
          { reasonList: 'xxxx', status: true },
          { reasonList: 'xxxx', status: false },
        ],
      },
      { cId: 'c-10005', uid: 'GYS-00905', name: 'ttt' },
      { cId: 'c-10006', uid: 'GYS-00906', name: 'qqq' },
    ],
  },
  {
    code: 1003,
    name: 'wangwu',
    detailList: [
      { cId: 'c-10007', uid: 'GYS-00907', name: 'mm' },
      { cId: 'c-10008', uid: 'GYS-00908', name: 'nn' },
    ],
  },
]
阅读 977
1 个回答
dataList.forEach(v => {
    v.detailList.forEach(o => ajaxBatchValidate({cId: o.cId,uid:o.uid}).then(res => o.ValidateList = res))    
})

看你标签还选了vue,如果是vue里做渲染的话就不能直接赋值要用this.$set去赋值新的对象属性。
ps: 其实我觉得这个应该后端连表查询后一起返回

已参与了 SegmentFault 思否社区 10 周年「问答」打卡 ,欢迎正在阅读的你也加入。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题