一、实现 trim
function trim(){
return this.replace(/^\s+|\s+$/,'')
}
二、数组去重
1. hash 表
function unique(array){
let temp = []
let hash = {}
for (let i=0;i<array.length;i++){
if(!hash[array[i]]){
hash[array[i]] = true
temp.push(array[i])
}
}
return temp
}
let array = unique([3,4,6,7,6,5,4,4,3,5,6])
console.log(array)
// [3, 4, 6, 7, 5]
2. new Set()
let array = [...new Set([3,4,6,7,6,5,4,4,3,5,6])]
console.log(array)
// [3, 4, 6, 7, 5]
「@浪里淘沙的小法师」