How do I capitalize the first letter of each word in an array of strings? Writing a `for … of … loop ` (ES6)
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 | const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; for (const day of days) { console.log(day); } |
我需要用大写的第一个字母打印日子…
试试:
1 2 | function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1);} |
1 | days.map(day => day[0].toUpperCase() + day.substr(1)) |
旧的学校:
1 2 3 4 5 6 7 8 | const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; var result = []; for(var i = 0; i < days.length; i++){ result.push(days[i].charAt(0).toUpperCase() + days[i].substring(1)); } console.log(result); |
使用的功能和
1 2 3 | const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; var result = days.map(d => d.replace(/(.?)/, (letter) => letter.toUpperCase())); console.log(result); |
你可以简单的回路,在第一天,得到两个字符uppercase这样:
1 2 3 4 5 | const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; for (const day of days) { console.log(day[0].toUpperCase() + day.substr(1)); } |
希望它helps
1 | string.charAt(0).toUpperCase() + string.slice(1); |