將 map() 函式應用于變數,str以便字串中的每個第二個單詞都轉換為大寫。在 JavaScript 中。
str = "hello my name is jon and i live in. canada."
uj5u.com熱心網友回復:
"hello my name is jon and i live in. canada."
.split(" ")
.map((word, idx) => idx % 2 === 0 ? word : word.toUpperCase())
.join(" ")
訣竅是:
- 以空格分隔。
- 帶索引的映射,但僅使用
toUpperCase()ifidx % 2 !== 0。 - 然后
join用空格回傳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405974.html
標籤:
下一篇:進行keras預測后的嵌套串列
