獲取數組裡英文名稱的第一個字母,並轉為大寫
阿新 • • 發佈:2021-01-07
技術標籤:js
var arry = [ {username:'Ancd Xeds'}, {username:'Bncd Xeds'}, {username:'Cncd Xeds'}, ]; arry.forEach((list, idx) => { // 獲取名稱第一個字母 var str = list.username; var arr = str.split(" "); var fristCode = ""; arr.forEach(function(v, i) { fristCode += v.substring(0, 1); }); // toUpperCase() -- 轉大寫 list.capital = fristCode.toUpperCase(); }); console.log(arry)