js把下划线字符串转为大驼峰字符串

let str = "zifu_chuan_chang"

function func(str)
{
    let arr = str.split('_')
    
    let resStr = arr.reduce(function(prev, cur){
        let str = prev + cur.slice(0, 1).toUpperCase() + cur.slice(1)
        return str
    })

    // 转小驼峰这一行不需要
    resStr = resStr.slice(0, 1).toUpperCase() + resStr.slice(1)

    return resStr
}

console.log(func(str))

posted on 2022-03-28 20:51  GameCat  阅读(590)  评论(0编辑  收藏  举报

导航