js 数组对象 去重

      let datas = [];
      let obj = {};
      // 去重 重复的ip
      datas = data.items.reduce(function (item, next) {
        obj[next.service] ? '' : (obj[next.service] = true && item.push(next));
        return item;
      }, []);

  

      // datas新数组 去重 重复的ip
      datas = data.items.reduce(function (item, next) {
        // 不符合条件 空数组 符合条件 push进空数组
        console.log(item.length, '我正在测试 item next 1', next);
        // obj 新对象
        // 三元表达式 前面== 后面=    重复的话 新对象的key值为''    否则 原数组先清空 再自身对比原数组里的元素
        // item 是把找到的next 单独放进空数组 item
        // obj[next.service] == true
        //   ? ''
        //   : (obj[next.service] = true && item.push(next)); // item 上一次 next 元素
        // console.log(item.length, '我正在测试 item next 2', next);

        return item;
      }, []);

  

posted on 2022-09-05 17:08  左侧岚  阅读(37)  评论(0编辑  收藏  举报

导航