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 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题