weixin_58412143 2022-12-21 13:51 采纳率: 91.6%
浏览 33
已结题

js将数组对象里的元素进行改变并重新赋值

怎么把这种数据tempUser改成下面这种shopList,我改完发现重复了

tempUser:[
  0:{
goodsId: (...)
image: (...)
isStock: (...)
num: (...)
price: (...)
shopId: (...)
skuNames: (...)
spuId: (...)
stock: (...)
totalPrice: (...)
}
1:{
goodsId: (...)
image: (...)
isStock: (...)
num: (...)
price: (...)
shopId: (...)
skuNames: (...)
spuId: (...)
stock: (...)
totalPrice: (...)
}
]

    "shopList": [
        {
            "shopId": 1,
            "goodsList": [
                {
                    "goodsId": 1,
                    "spuList": [
                        {
                            "spuId": 1,
                            "num": 12
                        },
                        {
                            "spuId": 2,
                            "num": 10
                        }
                    ]
                },
                {
                    "goodsId": 3,
                    "spuList": [
                        {
                            "spuId": 10,
                            "num": 15
                        }
                    ]
                }
            ]
        },
        {
            "shopId": 2,
            "goodsList": [
                {
                    "goodsId": 1,
                    "spuList": [
                        {
                            "spuId": 1,
                            "num": 12
                        },
                        {
                            "spuId": 2,
                            "num": 10
                        }
                    ]
                },
                {
                    "goodsId": 3,
                    "spuList": [
                        {
                            "spuId": 10,
                            "num": 15
                        }
                    ]
                }
            ]
        }
    ]

我改的——

 this.tempUser.map(item => {
            const obj2 = {
              spuId: item.spuId,
              num: item.num
            }
            this.arr2.push(obj2)
          })
          console.log('tempUser', this.tempUser)//ok
            var good = this.tempUser.map(o => o.goodsId === this.tempUser[0].goodsId);
            var type = good[good.length - 1]
            console.log('good', type)
          // goodsList
          if (type === true) {
            //不同规格
            this.tempUser.map(item => {
              const newOut1 = {
                goodsId: item.goodsId,
                spuList: this.arr2
              }
              this.goodsList.push(newOut1)
            })
          } else {
            //不同商品
            for (var i = 0; i < this.tempUser.length; i++) {
              const newOut1 = {
                goodsId: this.tempUser[i].goodsId,
                spuList: [this.arr2[i]],
              }
              this.goodsList.push(newOut1)
            }
          }
            this.goodsList = this.deletefunc1(this.goodsList);
    console.log('goodsList',this.goodsList)
            this.tempUser.map(item => {
              const newOut = {
                shopId: item.shopId,
                goodsList: this.goodsList
              }
              this.tempList.push(newOut)
            })
            this.tempList = this.deletefunc(this.tempList);
deletefunc1(arrayList) {
        const res = new Map();
        return arrayList.filter((arrayList) => !res.has(arrayList.goodsId) && res.set(arrayList.goodsId, 1)); //过滤
      },
      deletefunc(arrayList) {
        const res = new Map();
        return arrayList.filter((arrayList) => !res.has(arrayList.shopId) && res.set(arrayList.shopId, 1)); //过滤
      },
效果——
[0:{
goodsList: Array(2)
shopId: 1
},1{
goodsList: Array(2)
shopId: 2}
]
  • 写回答

1条回答 默认 最新

  • weixin_58412143 2022-12-21 14:30
    关注

    OK解决

    1————
        for (var i = 0; i < this.tempUser.length; i++) {
                                const newOut1 = {
                                    goodsId: this.tempUser[i].goodsId,
                                    spuList: [this.arr2[i]],
                                }
                                this.goodsList.push(newOut1)
                            }
                        } else {
                            //不同商品
    2————
    for (var i = 0; i < this.tempUser.length; i++) {
                            const newOut = {
                                shopId: this.tempUser[i].shopId,
                                goodsList: [this.goodsList[i]],
                            }
                            this.tempList.push(newOut)
                        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)