当前位置: 首页 > Web前端 > HTML

js终极去重方法

时间:2023-03-28 18:56:25 HTML

终极去重方法functiondereplication(arr){letobj={}returnarr.filter((item,index,arr)=>{returnobj.hasOwnProperty(typeofitem+item)?false:obj[typeofitem+item]=true;})}letarr=[1,1,'true','true',true,true,15,15,false,false,undefined,undefined,null,null,NaN,NaN,'NaN',0,0,'a','a',{},{}];dereplication(arr)这个方法直接使用filter来过滤判断对象中是否存在某个属性hasOwnProperty实现了,在hasOwnProperty中需要判断类型加值是否存在