我有一个字符串数组,其值在蛇的情况下。
我需要它们来映射一些输入。
如何转换
'some_string'
转换为 'Some String'
?
最佳答案
map()
在输入数组上-
split('_')
:将每个_
上的字符串转换为数组
-
map()
要制作的数组 each first char uppercase -
join(' ')
:将数组转换回字符串,并连接 在空格上 (
-
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);
输入数组:
[ 'some_string', 'foo_bar' ];
将产生:
[
"Some String",
"Foo Bar"
]
关于javascript - 如何将 Snake 案例转换为 Start 案例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69302491/