javascript - JS 对象循环返回未定义,而 Lodash 映射返回值
问题描述
我在 JS 中有一个循环,一旦循环完成,值将返回为 null,如果我出于相同目的使用 lodash,它会返回结果。
这是我尝试过的:
JS:
const jsRows = Object.entries(response).forEach(([key, row]) => {
return row;
});
console.log(jsRows) ==> Output undefined
罗达什:
const loRows = map(response, row => {
return row;
});
console.log(loRows) ==> Output Array
解决方案
该.forEach()
函数总是返回undefined
。它与.map()
函数完全不同。本机 JavaScriptArray.prototype.map()
还返回一个新数组。
该.forEach()
函数用于在不需要包含每个数组值转换的新结果的情况下对数组的每个元素“做某事”。.map()
另一方面,该函数用于基于每个数组元素的转换将数组转换为新数组。
推荐阅读
- reactjs - 如果 React 不接受,则重定向到隐私政策协议
- python - zlib解压压缩后得到与原文件不同的大小
- azure-active-directory - 使用 Power Automate 我需要在受 Azure AD 保护的站点上执行 HTTP 请求
- redux - 为什么我的 Observable 发出的值比预期的要多,为什么 auditTime 是一个修复?
- excel - 特定的 Excel 公式不起作用
- javascript - 如何在 TypeScript 中访问 Web 浏览器的 (Web API) 控制台 API 类型?
- c# - 从 EventHandler 中删除除前两个参数外的所有参数
- javascript - “UnhandledPromiseRejectionWarning:DiscordAPIError:无法向该用户发送消息”
- python - 将 2D 列表转换为 1D 分组值列表的更好方法
- python - 使用 glob、os、shutil 和 tkinter 使用 python 的桌面管理器