How can I convert Snake case into Start case?

Viewed 298

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'?

2 Answers
  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"
]
Related