javascript数组去重

数组去重原理

遍历数组,判断对象上是否有数组值是这个属性,有不执行,没有数组值作为对象的属性

直接上代码

Array.prototype.unique = function() {  //哈希 hash
  var temp = {},
     arr = [],
     len = this.length
  for(var i = 0; i < len; i++) {
    if (!temp[this[i]]) {
      temp[this[i]] = 'abc'
      arr.push(this[i])
    }
  }
  return arr
}
原文地址:https://www.cnblogs.com/lyjfight/p/13826992.html