一、实现 trim

  1. function trim(){
  2. return this.replace(/^\s+|\s+$/,'')
  3. }

二、数组去重

1. hash 表

  1. function unique(array){
  2. let temp = []
  3. let hash = {}
  4. for (let i=0;i<array.length;i++){
  5. if(!hash[array[i]]){
  6. hash[array[i]] = true
  7. temp.push(array[i])
  8. }
  9. }
  10. return temp
  11. }
  12. let array = unique([3,4,6,7,6,5,4,4,3,5,6])
  13. console.log(array)
  14. // [3, 4, 6, 7, 5]

2. new Set()

  1. let array = [...new Set([3,4,6,7,6,5,4,4,3,5,6])]
  2. console.log(array)
  3. // [3, 4, 6, 7, 5]

「@浪里淘沙的小法师」