javascript - 在javascript中使用箭头函数返回名字和姓氏的首字母
问题描述
在 javascript 中使用箭头函数时,尝试让控制台日志显示名字和姓氏的首字母。
const getInitials = (firstName, lastName) => {
firstName + lastName;
}
console.log(getInitials("Charlie", "Brown"));
解决方案
您必须return
在大括号内指定。您可以使用charAt()
以下方式获取首字母:
const getInitials = (firstName,lastName) => { return firstName.charAt(0) + lastName.charAt(0); }
console.log(getInitials("Charlie", "Brown"));
或:您不需要return
if 删除大括号:
const getInitials = (firstName,lastName) => firstName.charAt(0) + lastName.charAt(0);
console.log(getInitials("Charlie", "Brown"));
推荐阅读
- sonata-admin - How can I add a currency field in list with currency string coming from data?
- javascript - 如何从车把助手发出ajax请求?
- r - Comparing Dataframe column names in R
- .net-core - 使用外部和内部 URL 的身份服务器
- php - 如何根据php中的键值从mysql数据库中获取数据
- google-api - 如何使用 Postman 和 HTTPS 使用 Google 服务帐户凭据进行身份验证?
- c# - 尝试在 XAML 故事板动画中使用用户控件的自定义属性会引发无法解析 TargetProperty 错误
- python - 如何在 Python 中对大数进行正确除法?
- python - 在 pandas 中使用地理编码器 api 查找位置
- wpf - 用于两个触发器的模板属性设置器