我有这个对象:
{
id: 1,
nums: 3,
dates: [
{
date: '21-10-2012',
promo: {
id: 1,
name: 'promo 1'
},
hours: [
{
hour: '22:00',
vision: {
id: 1,
name: '2D'
}
},
{
hour: '23:00',
vision: {
id: 2,
name: '3D'
}
}
]
},
{
date: '21-10-2012',
promo: {
id: 2,
name: 'promo 2'
},
hours: [
{
hour: '22:00',
vision: {
id: 2,
name: '3D'
}
}
]
},
{
date: '21-10-2013',
promo: {
id: 2,
name: 'nome promo'
},
hours: [
{
hour: '22:00',
vision: {
id: 2,
name: '3D'
}
}
]
}
]}
我想合并具有相同日期值以及这些覆盖促销和时间的对象。下面是我想要的结果:
{
id: 1,
nums: 3,
dates: [
{
date: '21-10-2012',
promo: {
id: 2,
name: 'promo 2'
},
hours: [
{
hour: '22:00',
vision: {
id: 2,
name: '3D'
}
},
{
hour: '23:00',
vision: {
id: 2,
name: '3D'
}
}
]
},
{
date: '21-10-2013',
promo: {
id: 2,
name: 'nome promo'
},
hours: [
{
hour: '22:00',
vision: {
id: 2,
name: '3D'
}
}
]
}
]}
我试着做点什么,但我不知道怎么做:
obj.dates.reduce(function(acc, curr) {
if (new Date(acc.date).valueOf() === new Date(curr.date).valueOf()) {
acc.hours = acc.hours.reduce(function(acc, curr) {
return Object.assign(acc, curr);
});
}
return Object.assign(acc, curr);
}, {})