说到数组去重,其实大家都不陌生
传统型数组去重的其中一种方法:
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;}
那么在ES6下也有一个数组去重的方法 Set:
var set = new Set();
console.log(set([1,2,3,3])) //[1,2,3] ;
但是有一点要注意的是。Set方法并不能把数组对象去重,例如:
var arr = {[a:"apple"],[a:"apple"],[a:"apple"],[b:"boy"]};
console.log(set(arr)) //{[a:"apple"],[a:"apple"],[a:"apple"],[b:"boy"]} ;
事实证明,ES6下的Set去重只能去重基本数据类型。