1. 程式人生 > 其它 >獲取數組裡英文名稱的第一個字母,並轉為大寫

獲取數組裡英文名稱的第一個字母,並轉為大寫

技術標籤: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)

在這裡插入圖片描述