Encapsulation of deep copy, initial capitalization, simple array deduplication method

Encapsulation of deep copy, initial capitalization, simple array deduplication method

1. Deep copy

  function deepCopy(data) {
    
    
    const t = typeof (data)
    let o
    if (t === 'array') {
    
    
      o = []
    } else if (t = 'object') {
    
    
      o = {
    
    }
    } else {
    
    
      return data
    }
    if(t === 'array'){
    
    
      for(let i = 0; i < data.length; i++){
    
    
        o.push(deepCopy(data[i]))
      }
    }else if(t === 'object'){
    
    
      for(let i in data){
    
    
        o[i] = deepCopy(data[i])
      }
    }
    return o
  }

2. Capitalize the first letter

function fistLetterup(str){
    
    
  return str.charAt(0).toUpperCase() + str.slice(1)
}

3. Simple array de-duplication

function uniq(arr){
    
    
  const resArr = []
  arr.forEach(ele => {
    
    
    if(resArr.indexOf(ele) === -1) {
    
    
      resArr.push(ele)
    }
  })
  return resArr
}

Guess you like

Origin blog.csdn.net/qq_43923146/article/details/112985589