给数组里的对象中的数组每一项添加一个属性,属性值为发送请求过后的值

这样的一个需求,下面

export const productObj = [
  {
    img: 'img1',
    data: [
      {
        id: 1,
        text: '1'
      }, {
        id: 2,
        text: '2'
      }, {
        id: 3,
        text: '3'
      }
    ]
  }, {
    img: 'img2',
    data: [
      {
        id: 4,
        text: '4'
      }, {
        id: 10042,
        text: '5'
      }, {
        id: 5,
        text: '6'
      }
    ]
  }
  ]

根据id请求数据 并把结果放在data数组中对应的对象中。

方法一:

// function onLoad() {
//   const infoList = _.cloneDeep(this.productObj)
//   const promiseArray = []

//   for (const i in infoList) {
//     promiseArray.push(this.test(infoList[i].data))
//   }

//   Promise.all(promiseArray).then(res => {
//     console.log(res)
//   })
// }

// function test(param) {
//   return new Promise((resolve, reject) => {
//     const promiseArray =
//       []
//     for (const
//       i in param) {
//       promiseArray.push(this.test2(param[i].id))
//     }
//     Promise.all(promiseArray).then(res => {
//       resolve(res)
//     })
//   })
// }

// function test2(param) {
//   return new Promise(async(resolve, reject) => {
//     const res = await getPlatDetail(param)
//     resolve(res.data)
//   })
// }

方法二(简化后):

async promiseAllInfo() {
      const infoList = _.cloneDeep(this.productObj)
      const info = await Promise.all(
        infoList.map(item => {
          return new Promise(async resolve => {
            const result = await Promise.all(item.data.map(el => {
              return new Promise(resolve => {
                const result = this.getPlatDetail(el.id)
                resolve(result)
              })
            }))
            resolve(result)
          })
        })
      )
      this.infoList = infoList
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值