-1 javascript
是否有一种非向后的方法将一个数组索引转换为大写字符串?我不想拆分,循环或其他任何东西,因为我只需要这一个字符串.
colors = [ "red", "green", "blue" ];
red = colors[0];
redUpper = red.toUppercase();
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它toUppercase()是一个字符串函数.我知道.我只是在寻找实现这一目标的最简单方法.
Era*_*hel 10
如果您只想要第一个元素大写,则可以使用以下String#toUpperCase函数:
const colors = [ "red", "green", "blue" ];
const redUpper = colors[0].toUpperCase();
console.log(redUpper);Run Code Online (Sandbox Code Playgroud)
如果你想要它们,请使用Array#Map.
const colors = [ "red", "green", "blue" ];
console.log(colors.map(a => a.toUpperCase()));Run Code Online (Sandbox Code Playgroud)