javascript - ES6 迭代包含 const
问题描述
有人可以解释为什么我在这段代码中出现错误吗?顺便说一句,我在 es6 中还是新的,谢谢
selData.map((item,idx)=>({
const TargetItem = aclEntries.findIndex(rec=>rec.stakeholder_id===item.value)
console.log(TargetItem)
}))
解决方案
ES6 函数可以用几种不同的方式编写。
// If you have more than 1 param, you have to wrap them with `()`
const functionName = (param1, param2) => { return console.log(param1, param2); }
// If you're just returning a value, you can omit the `{}`
const functionName = (param1, param2) => console.log(param1, param2);
// If you only have 1 param, you can omit the `()` around the params
const functionName = param1 => console.log(param);
我在下面的代码中添加了另一个示例.find()
,它将为您提供 的结果aclEntries
,而不仅仅是索引位置。
另外,我使用forEach
而不是map
, asmap
创建了一个新数组,并且您没有将返回值分配给map
变量以供以后使用。
const aclEntries = [
{ stakeholder_id: '1234', taco: 'cat' }
];
const selData = [
{ value: '1234' }
];
selData.forEach((item) => {
const TargetItemIndex = aclEntries.findIndex(rec => rec.stakeholder_id === item.value);
const TargetItemContent = aclEntries.find(rec => rec.stakeholder_id === item.value);
console.log('index', TargetItemIndex);
console.log('content', TargetItemContent);
})
推荐阅读
- python - 使用 requests post 方法登录网站
- pcap - 需要日志 Pcap
- python - 从不同来源供电的 LCD 无法显示
- css - BS 4 切换图标交换
- asp.net-mvc - 似乎无法呈现部分视图 - 控制器没有被击中
- sql-server - SSMS 版本 18 – 无数据库图
- javascript - 未找到模块:错误:使用 webpack-hot-middleware 时无法解析 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true'
- html - React 错误:样式道具值必须是对象 react/style-prop-object
- panel - Drupal 8:从默认分类术语页面/路径重定向到在面板/页面中创建的自定义页面
- python - Pandas 动态条件格式