Home > Software design >  How can I convert Snake case into Start case? [duplicate]
How can I convert Snake case into Start case? [duplicate]

Time:09-23

I have an array of strings with values in the Snake case.

I need them to map some inputs.

How can I convert 'some_string' into 'Some String'?

CodePudding user response:

  1. map() over the input array

    1. split('_'): Convert string into an array on each _
    2. map() the array to make each first char uppercase
    3. join(' '): Convert the array back to a string, joined on a space ( )

const inputArray = [ 'some_string', 'foo_bar' ];
const outputArray = inputArray.map((input) => (
  input.split('_').map(s => s.charAt(0).toUpperCase()   s.slice(1)).join(' ')
));

console.log(outputArray);

Input array:

[ 'some_string', 'foo_bar' ];

Will produce:

[
  "Some String",
  "Foo Bar"
]

CodePudding user response:

You can do:

const arr = ['some_string', 'foo_bar']
const result = arr
  .map(str => str.split('_')
    .map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`)
    .join(' ')
  )


console.log(result)

  • Related