javascript - JavaScript forEach 方法不适用于箭头函数
问题描述
const array3 = [5, 10, 15, 20, 25, 30, 35, 40];
const function5 = (index, element) => {return index + ": " + element};
console.log(array3.forEach(function5));
为什么这段代码不起作用,我做错了什么?
解决方案
您需要使用.map()
as.forEach()
将始终返回您undefined
,并且可以通过使用map()
函数获得您期望的结果:
const array3 = [5, 10, 15, 20, 25, 30, 35, 40];
const function5 = (index, element) => {return index + ": " + element};
console.log(array3.map(function5));
推荐阅读
- c++ - 如何使用 opus 编解码器从 webm 文件中获取原始音频 pcm 数据
- transactions - 如何将所有处理器或组设置为一个整体以支持 Nifi 中的事务?
- c# - 有没有办法在 WPF 中使用带有 HTML 格式的 Textbox/RichTextBox?
- ruby-on-rails - nil:NilClass 的未定义方法“名称”:
- r - 有没有办法在 R 闪亮中应用动态过滤器,类似于在 excel 中的枢轴
- opengl - 当我在 OpenGL 中关闭渲染窗口时,为什么会发生“检测到堆栈粉碎”错误?
- postgresql - 如果插入依赖于先前的记录,如何处理对数据库的并发插入?
- java - 在服务器上以调试模式运行项目时发生 wildfly 超时 [300]
- logging - 如何记录来自 aiohttp 客户端的原始 POST 请求?
- c# - 如何在解决方案文件夹中添加文件?