js数组去重查重

数组去重
    var arr = [1, 2, 4, 4, 3, 3, 1, 5, 3, 2, 3, 3]
    var newarr = arr.filter(function (item, index, self) {
          return self.indexOf(item) === index
          })
查找重复元素
    var arr = [1, 2, 4, 4, 3, 3, 1, 5, 3, 2, 3, 3]
    方法1:
    var repeat = Array.from(new Set(arr.filter(function (item, index, self) {
          return self.indexOf(item) !== index
    })))
    方法2:
    var norepeat2 = arr.filter(function (item, index, self) {
          return self.indexOf(item) === index && self.lastIndexOf(item) !== index
    })
    方法3:
    var result = []
    arr.forEach(function (item, index) {
      if (result.indexOf(item) === -1) {
        result.push(item)
      }
      return result
    })

你可能感兴趣的:(js数组去重查重)