码迷,mamicode.com
首页 > 编程语言 > 详细

json格式下的数组去重

时间:2018-11-23 16:09:42      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:json格式   一个   set   pre   function   this   fun   ret   style   

    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());
  },

 

json格式下的数组去重

标签:json格式   一个   set   pre   function   this   fun   ret   style   

原文地址:https://www.cnblogs.com/xuhuang/p/10007935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!