使用map函数查找字符串中的第二个单词



将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(" ")

诀窍是:

  1. 按空格分隔
  2. 使用索引映射,但如果是idx % 2 !== 0,则仅使用toUpperCase()
  3. 然后join带空格返回

最新更新