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:
map()over the input arraysplit('_'): Convert string into an array on each_map()the array to make each first char uppercasejoin(' '): 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)
