获取到res.data.data下的数组里的数组

话不多说上代码

            // // 遍历数组获取里面的商品内容添加
            let list = [];
            this.data.orderfrom_list.map(element=>{
              let orderItems=[]
              let obj = {};
              obj.id = element.id;
              obj.storeName = element.storeName;
              orderItems.push(element.orderItem)
              obj.orderItem = orderItems;
              list.push(obj);
            })
            this.setData({
              order_Item:list,
            })
            console.log(list)

改变一下数据结构然后进行双重循环,我之前博客有写到

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我优化下 这段 js代码 我感觉写的好凌乱 variation(id, item, index) { console.log(index, ' index 124') console.log(id, ' 125') console.log(item, ' item') // 添加子元素 this.$api.mine.extendList({ id: id }).then((res) => { console.log(res.data.userExtendOne) if (this.treeData[index].child) { //push 1个人 if (res.data.userExtendOne && res.data.isLeader == 0) { console.log('1111') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ) } //push 2个人 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.isLeader == 0) { console.log('2222') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ) } //push 3个人 console.log(res.data.isLeader, '150----') if (res.data.isLeader == 1) { console.log('3333') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree }) } } else { //push 1 孩子的 孩子 if (res.data.userExtendOne) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ]) } //push 2 孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ]) } //push 3孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.userExtendThree) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree } ]) } } // this.treeData.push(this.removeDuplicates(this.treeData)) console.log("===data===" + JSON.stringify(this.treeData)) }) },
06-09
可以尝试优化以下这段代码: ``` async variation(id, item, index) { console.log(index, ' index 124'); console.log(id, ' 125'); console.log(item, ' item'); const res = await this.$api.mine.extendList({ id }); console.log(res.data.userExtendOne); const child = []; if (res.data.userExtendOne && res.data.isLeader === 0) { console.log('1111'); child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }); } if (res.data.userExtendOne && res.data.userExtendTwo && res.data.isLeader === 0) { console.log('2222'); child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }); } console.log(res.data.isLeader, '150----'); if (res.data.isLeader === 1) { console.log('3333'); child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree }); } if (child.length > 0) { if (this.treeData[index].child) { this.treeData[index].child.push(...child); } else { this.$set(this.treeData[index], 'child', child); } } console.log("===data===" + JSON.stringify(this.treeData)); }, ``` - 将异步请求改为使用 async/await; - 简化了添加子元素的逻辑; - 将添加子元素的重复代码提取到一个数组中,避免代码冗余; - 在添加子元素前,先判断 child 数组是否为空,避免在 push 时出现问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值