js数组对象去重

js数组对象去重:

<html>

<head>
  <title>我的第一个 HTML 页面</title>
</head>

<body>
</body>
<script>
  let obj = [{
      id: 1,
      type: 1,
    },
    {
      id: 2,
      type: 2,
    },
    {
      id: 2,
      type: 2,
    },
    {
      id: 2,
      type: 2,
    },
    {
      id: 2,
      type: 2,
    },
    {
      id: 3,
      type: 3,
    },
    {
      id: 1,
      type: 1,
    },
  ]
  let arry = [1, 2]

  function unique(arr) {
    const res = new Map();
    return arr.filter((arr) => !res.has(arr.type) && res.set(arr.type, 1));
  }
  obj = unique(obj);
  console.log(obj);
  obj.forEach(i => {
    arry.forEach(item => {
      if (item == i.type) {
        console.log(item)
      }
    })
  })
</script>

</html>

  

posted @ 2020-11-13 15:45  胡炖鱼  阅读(249)  评论(0编辑  收藏  举报