weixin_58412143 2022-12-21 13:51 采纳率: 92.1%
浏览 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日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法