我从一个端点回来这样的有效载荷:对象键添加到每个项目对象数组中
const charges = [
{
'id': 'someId',
'dates': ['2017-11-11', '2017-12-11'],
},
{
'id': 'anotherId',
'dates': ['2017-09-11', '2017-10-11'],
},
];
什么是对dates
阵列连接id
到每个项目的所以最好的办法这导致这样的:
[
{ id: 'someId', date: '2017-11-11' },
{ id: 'someId', date: '2017-12-11' },
{ id: 'anotherId', date: '2017-09-11' },
{ id: 'anotherId', date: '2017-10-11' },
]
我已经试过这样的事情:
let history = [];
charges.map(charge => (
charge.dates.map((date) => (
history.concat({
id: charge.payerCode,
date: date,
})
))
));
但我在使用它的范围中没有定义历史记录。
我知道可能有更好的方法来做到这一点,但我似乎无法绕过它。
任何帮助将不胜感激!
谢谢!
你想输出什么? –
请告诉我你期望输出的例子是什么? – Microsmsm
将问题添加到期望的输出。抱歉! –