如何使用角度提取第一个单词的第一个字母和第二个单词的第一个字母

时间:2021-04-09 19:41:10

标签: html css angular

我有从 ts 到 HTML 的 fullName,但在视图中我必须只显示名字的首字母和姓氏的首字母。例如

这是我的html代码

{fullName}

这是我的 ts 代码

fullName = John Doe

在输出中这是我正在寻找的

JD

1 个答案:

答案 0 :(得分:3)

你可以这样做:

const fullName = "Admin k L Name";

const split = fullName.split(" ");
const output = split[0][0] + split[1][0];

console.log(output);




如果你想重用它,创建一个函数,就像这样:

const fullName = "Admin k L Name";

const nameReducer = (name) => {
  const split = fullName.split(" ");
  return split[0][0] + split[1][0];
};

const output = nameReducer(fullName); // ? You can add this to the div
console.log(output);



<强>????:??????????????????????????????????? ???????。