json格式下的数组去重

    var users = [{
      id: 1, name: "a"
    }, {
      id: 2, name: "a"
    }, {
      id: 3, name: "b"
    }, {
      id: 4, name: "v"
    }]
    Array.prototype.unique = function () {
      var res;
      var arr = this.map(item => {
        return this[item.id - 1] = item.name
      })
      // ES6里新添加了两个很好用的东西,set和Array.from
      // set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目。
      res = new Set(this);
      console.log("new Set对象", res)
      res = Array.from(new Set(this));
      return res//es6 数组去重
    }
    console.log(users.unique());
  },

猜你喜欢

转载自www.cnblogs.com/xuhuang/p/10007935.html
今日推荐