dengnan123 / A-question-before-going-to-bed

睡前一题
0 stars 0 forks source link

写一个方法把下划线命名转成大驼峰命名 #1

Open dengnan123 opened 4 years ago

dengnan123 commented 4 years ago
const str3 = 'test_dany_ha_c_'

function func(str) {
  const arr = str.split('_')
  const arr1 = [arr[0]]
  for (let i = 1; i < arr.length; i++) {
    const v = arr[i]
    if (v.length) {
      const key = v[0]
      const newStr = v.replace(`${key}`, key.toUpperCase())
      arr1.push(newStr)
    }
  }
  return arr1.join('')
}

console.log('test', func(str3))
dengnan123 commented 4 years ago

更多解法 https://github.com/haizlin/fe-interview/issues/12