数组去的推荐写法

思路

对象 相比于数组查找元素具有天生的优势 因为 对象的数据 是以键值存在的 非常容查找 不用循环遍历

实例

对数组 [ 1,2,3,33,44,1,2,3]

    var arr1 = [ 1,2,3,33,44,1,2,3]

    Array.prototype.unique = function () {
  	var  res = []
  	var  json = {}

  	for ( var i=0; i<this.length; i++){
  		if(!json[this[i]]){
  			res.push(this[i])
  			json[this[i]] = 1 
  		}

  	}
  	return res 
    }

 console.log(arr1.unique())

posted @ 2017-04-19 15:57  wkm-wangZhe  阅读(164)  评论(0编辑  收藏  举报