题解 | #数组去重#
数组去重
https://www.nowcoder.com/practice/0b5ae9c4a8c546f79e2547c0179bfdc2
思路:使用当前数组this构造一个Set对象,再将Set对象析构为数组返回即可。
Array.prototype.uniq = function () { //使用new Set去重 this指向的是当前数组 let uniqset=new Set(this) //使用析构...将set再转换为数组 或者使用Array.from(uniqset) return [...uniqset] }
总结:new Set()方法创建set对象,new Set(arr)使用arr创建set对象,[...set]将set转换为arr,Array.from(set)将set转换为arr。
#数组去重defremovesamelnewlforiinlifinotinnewlnewlappendireturnnewl#前端js面试 文章被收录于专栏
前端js面试,帮助你更好的理解js。