js数组去重,常用

// 数组去重
    removeDuplicats(arr, type) {
      if (arr.length == 0) {
        return arr
      } else {
        if (type) {
          var obj = {}
          var newArr = arr.reduce((cur, next) => {
            obj[next[type]] ? '' : (obj[next[type]] = true && cur.push(next))
            return cur
          }, [])
          return newArr
        } else {
          return Array.from(new Set(arr))
        }
      }
    },

使用:

 this.selectUserList = this.removeDuplicats(
              this.selectUserList, // 去重的数组
              'USERID' // 根据这个USERID去重
            )
javascript
85 views
Comments
登录后评论
Sign In
·

nice!还可以直接用es6的set和扩展运算符:

[...new Set(数组名)]