使用 map 函数定位字符串中的第二个单词

target ever second word in a string using map function

将 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.
  3. 时使用 toUpperCase()
  4. 然后 join 返回空格。