How to convert a camel-case string to dashes in JavaScript?

前端 未结 7 1277
情深已故
情深已故 2021-02-13 02:22

I want to convert these strings:

fooBar
FooBar

into:

foo-bar
-foo-bar

How would I do this in JavaScript the m

7条回答
  •  不思量自难忘°
    2021-02-13 03:20

    You can use

    const makeItDashed = camelCased => {
       let dashed = ``
       camelCased.split(``).map(ch => {{dashed += ch.toUpperCase() == ch ? `-${ch.toLowerCase()}` : ch}})
       return dashed
    }
    
    console.log(makeItDashed(`fooBar`))
    console.log(makeItDashed(`FooBar`))
    

提交回复
热议问题