# 前端技术(二)

# 实现数组去重

# 第一种方法(Map 记录)

1
2
3
4
5
6
7
8
9
10
11
12
function qucong1(arr)
{
let newArr=[];
arr.reduce((prev,next)=>{
if(!prev.has(next))
{
prev.set(next,1)
newArr.push(next)
}
},new Map())
return newArr;
}

# 第二种方法(set 去重)

1
2
3
4
function quchong2(arr)
{
return [... new Set(arr)]
}

# 实现一个 compose 函数

1
2
3
4
5
6
7
8
9
function compose(...func){
if(func.length===0) return (num)=>num;
if(func.length===1) return func[0];
return
func.reduce((prev,next)=>{
return (num)=>{ return next(prev(num))}
})

}

# 实现数组转化为树节点对象

# 第一种方法用递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function arrayToTree(data,rootId)
{
function getChildren(data,result,id)
{
for(const item of data)
{
if(item.pid===id)
{
let newItem={...item,children:[]};
result.push(newItem)
getChildren(data,newItem.children,item.id)
}
}
}
let result=[];
getChildren(data,result,rootId)
return result;
}

# 手机号脱敏

1
2
3
export const hiddenmoblie=(moblie)=>{
return mobile.replace(/^(\d{3})\d{4}(\d{4})$/)
}