ES6下的数组去重

说到数组去重,其实你们都不陌生json

传统型数组去重的其中一种方法:数组

Array.prototype.unique3 = 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;app

}this

 

那么在ES6下也有一个数组去重的方法 Set:prototype

 

var set = new Set();对象

console.log(set([1,2,3,3])) //[1,2,3] ;io

可是有一点要注意的是。Set方法并不能把数组对象去重,例如:console

var arr = {[a:"apple"],[a:"apple"],[a:"apple"],[b:"boy"]};function

console.log(set(arr)) //{[a:"apple"],[a:"apple"],[a:"apple"],[b:"boy"]} ;数据类型

事实证实,ES6下的Set去重只能去重基本数据类型。

相关文章
相关标签/搜索