字符串toUpperCase()的数组不起作用

问题描述 投票:0回答:2

我有以下字符串数组

let example = ["hello", "what"];

我想将此数组转换为此:

 ["Hello", "what"];

为了完成我有以下代码:

example[0][0] = example[0][0].toUpperCase();

[当我尝试console.log(示例)时,我得到了

["hello", "what"]

这里怎么了?如何将第一个元素的首字母转换为大写?

javascript arrays
2个回答
1
投票

您为什么有两个方括号?您只需要一个...

example[0]=example[0].toUpperCase();

1
投票

我希望能有所帮助。

let example = ["hello", "what"];

example = example.map(ar => ar[0].toUpperCase() + ar.slice(1));
© www.soinside.com 2019 - 2024. All rights reserved.