javascript - 以下代码片段中的最小更改是什么,以使输出为“ABC”?
问题描述
您好,我的功能有这个问题
const string = ['a', 'b', 'c'].reduce((acc, x) => x.concat(x.toUpperCase()));
console.log(string );
在最终结果中我想得到“ABC”
解决方案
你需要做两件事
- 适用
concat()
于acc
不与x
- 通过将其作为第二个参数传递来设置
acc
to的初始值''
reduce()
- 您可以使用
+
而不是contat()
const string = ['a', 'b', 'c'].reduce((acc, x) => acc+x.toUpperCase(),'');
console.log(string );
您也可以使用map()
和join()
const string = ['a', 'b', 'c'].map(x=>x.toUpperCase()).join('')
console.log(string );
推荐阅读
- r - 错误:列“Fecha”无法从 POSIXct、POSIXt 转换为字符(在 R 中)
- mysql - 我如何结合两列来过滤在两者之间使用?
- java - 需要从 Mono 中提取数据
- asp.net-web-api - 对 .NET WebApi HttpBinding 的错误请求进行故障排除
- include - 使用 vcpkg 的 PCL 库设置似乎缺少包含文件(尤其是 pcl/可视化)
- python - 如何将在 Flask 服务器上创建的 PIL Image 下载到我的桌面。Image.save() 仅将其保存到服务器。无法通过 URL 访问图像
- java - 使用 Calendar.HOUR_OF_DAY 的条件语句
- ios - Xamarin 应用程序能否确定它在 iOS 14 上是否具有 LAN 访问权限?
- flutter - 从 TextField 中删除所有字符后,flutter_typeahead 未找到建议
- ios - 获取 ERROR ERROR [object Object] 但仅在 ios 上。在电脑上工作正常