你好,我有以下数据:
data = [{due_at: '2019-09-10', yards:[{name: 'test'},{name: 'test2'}]}...]
我希望能够遍历这个日期以得到以下结果:
events = [{start: '2019-09-10', end: '2019-09-10, title: 'test'},{start: '2019-09-10', end: '2019-09-10, title: 'test2'}...]
我尝试使用以下嵌套映射:
events = data.map(d => {
return d.yards.map(y => {
return {
start: d.due_at,
end: d.due_at,
title: y.name
};
});
});
这在某种程度上有效,但我一直得到类似于嵌套数组的返回结果:
[[{start: '2019-09-10', end: '2019-09-10, title: 'test'},{start: '2019-09-10', end: '2019-09-10, title: 'test2'}...]]
如何调整我的地图代码以输出单个对象数组?
.flatMap()
是JS的一个新功能,目前并不被所有浏览器支持。我加了一个版本,使用.reduce()
,它的浏览器支持更好。 - Nick Parsons