![](/img/trans.png)
[英]how can I return all the elements of an array with the first letter as a capital letter
[英]How to join the elements of an array such that the first letter is capital for all elements in JavaScript?
我想加入一個數組的元素,用逗號分隔,每個元素的第一個字母是大寫的,這不應該影響 object 值本身。
例如:
obj= ["蘋果","橙子","檸檬"]
Output 應該是:Apple、Orange、Lemon。
這是我嘗試過但失敗了的:
obj.charAt(0).toUpperCase().join(', ')
這是代碼:
let a = ['apple', 'orange', 'lemon'];
let res = a.map(fruit => fruit.charAt(0).toUpperCase() + fruit.slice(1));
console.log(res); // [ "Apple", "Orange", "Lemon" ]
const fruits = ["apple", "orange", "lemon"];
const new_fruits =
fruits
.map(x => x.replace(/^[a-z]/, (ch) => ch.toUpperCase()))
.join(", ");
替換第一個字母 az,將其大寫,最后用逗號連接。 可讀性 ftw。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.