将map((函数应用于变量str
,使字符串中的每一个单词都转换为大写。在javascript中。
str = "hello my name is jon and i live in. canada."
"hello my name is jon and i live in. canada."
.split(" ")
.map((word, idx) => idx % 2 === 0 ? word : word.toUpperCase())
.join(" ")
诀窍是:
- 按空格分隔
- 使用索引映射,但如果是
idx % 2 !== 0
,则仅使用toUpperCase()
- 然后
join
带空格返回