Convert dash-separated string to camelCase?

后端 未结 13 1227
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 11:01

For example suppose I always have a string that is delimited by \"-\". Is there a way to transform

it-is-a-great-day-today

to

itIsAGreatDayToday

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 11:45

    'it-is-a-great-day-today'.split('-').map(function(x,i){
        return (i?x[0].toUpperCase():x[0]) + x.slice(1).toLowerCase()
    }).join('')
    

    Result:

    'itIsAGreatDayToday'
    

    Alternatively, .match(/\w+/g) rather than .split('-') -- depending on what you want to do in edge cases like "this--is-a-test".

提交回复
热议问题