抱着猫睡觉的鱼_ 2021-09-26 14:36 采纳率: 51.7%
浏览 111
已结题

vue数组对象如何去重复

数据格式如下:

let arr = [
    {
        "id": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "S",
        "skuCode": "10",
        "skuName": "美年达1",
        "price": 10,
        "isShelf": "上架",
        "check": false
    },
    {
        "id": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "M",
        "skuCode": "11",
        "skuName": "可乐1",
        "price": 11,
        "isShelf": "上架",
        "check": false
    },
    {
        "id": "57bc93ad-84de-4737-8ca6-d6abe97eb9ae",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "L",
        "skuCode": "12",
        "skuName": "雪碧1",
        "price": 12,
        "isShelf": "上架",
        "check": false
    },
    {
        "id": "57bc93ad-84de-4737-8ca6-d6abe97eb9ae",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "L",
        "skuCode": "12",
        "skuName": "雪碧1",
        "price": 12,
        "isShelf": "上架",
        "check": false
    },
    {
        "id": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "M",
        "skuCode": "11",
        "skuName": "可乐1",
        "price": 11,
        "isShelf": "上架",
        "check": false
    },
    {
        "id": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "S",
        "skuCode": "10",
        "skuName": "美年达1",
        "price": 10,
        "isShelf": "上架",
        "check": false
    }
]

目前是id是每一条数据的唯一,如何利用id去重
想要去重后的格式:

[
    {
        "id": "46f7c6ba-b4ca-4843-b5d1-76399d1ac52a",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "S",
        "skuCode": "10",
        "skuName": "美年达1",
        "price": 10,
        "isShelf": "上架",
        "check": true
    },
    {
        "id": "2ea2a723-1a6c-4545-92ce-2ef07547642f",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "M",
        "skuCode": "11",
        "skuName": "可乐1",
        "price": 11,
        "isShelf": "上架",
        "check": true
    },
    {
        "id": "57bc93ad-84de-4737-8ca6-d6abe97eb9ae",
        "parentId": "a80ff169-d6f6-4752-b980-fd818240410f",
        "attributeName1": "颜色",
        "attributeValue1": "黄色",
        "attributeName2": "尺寸1",
        "attributeValue2": "L",
        "skuCode": "12",
        "skuName": "雪碧1",
        "price": 12,
        "isShelf": "上架",
        "check": true
    }
]

  • 写回答

4条回答 默认 最新

  • 一只成序源 2021-09-26 14:43
    关注
    
    let obj={};
    let newArray=[];
    arr.forEach(item=>{
     
      if(!obj[item.id]){
         obj[item.id]=true;
         newArray.push(item)
    }
    })
    console.log(newArray)
    

    这个可以直接采纳了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月4日
  • 已采纳回答 9月26日
  • 修改了问题 9月26日
  • 创建了问题 9月26日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器