JS45 数组去重
描述
为 Array 对象添加一个去除重复项的方法
示例1
输入:
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]
复制输出:
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']
Array.prototype.uniq = function () {return [...new Set(this)];// Set对象允许存储任何类型的唯一值,可以直接利用set把数组去重,但它返回的是类数组,所以要在转为数组。
}
Set 对象 允许存储任何类型的唯一值,可以直接利用set把数组去重,但它返回的是类数组,所以要在转为数组。