Javascript ES6映射在对象数组上抛出错误
我的代码obj没有定义,我的对象数组如下Javascript ES6映射在对象数组上抛出错误,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我的代码obj没有定义,我的对象数组如下 const normalizeEventTypes = nextProps.events.space_events.map(obj, i => obj.id ) 我遗漏了什么吗?您忘记使用(),请这样写: { "space_events": [{ "id": 1, "name": "Anniversaries" }, { "
const normalizeEventTypes = nextProps.events.space_events.map(obj, i =>
obj.id
)
我遗漏了什么吗?您忘记使用()
,请这样写:
{
"space_events": [{
"id": 1,
"name": "Anniversaries"
},
{
"id": 2,
"name": "Brand Rollout"
}
}]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) =>
obj.id
)
a = b.map((i,j) => i)
原因:
在map
回调函数中使用obj和index这两个参数,因此需要使用()
来包装参数,如下所示:
{
"space_events": [{
"id": 1,
"name": "Anniversaries"
},
{
"id": 2,
"name": "Brand Rollout"
}
}]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) =>
obj.id
)
a = b.map((i,j) => i)
当我们只想使用一个参数时,这些()
是可选的,如下所示:
{
"space_events": [{
"id": 1,
"name": "Anniversaries"
},
{
"id": 2,
"name": "Brand Rollout"
}
}]
}
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) =>
obj.id
)
a = b.map((i,j) => i)
地图的不同使用方式:
1.a.map(i=>i+1)//不需要索引时
2.
a = b.map(i => i)
3.a.map((i,j)=>i+j)//何时要使用项的索引
检查工作代码段:
let数据={
“空间活动”:[{
“id”:1,
“姓名”:“周年纪念日”
},
{
“id”:2,
“名称”:“品牌展示”
}
]
}
让result=data.space\u events.map((obj,i)=>obj.name);
console.log('result',result)代码>控制台中有错误吗?注意下面的@MayankShukla。如果问题确实解决了,一定要接受它。