0

我有这个对象:

{
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);
}, {})
4

1 回答 1

1

解决!希望它对某人有用!

let obj = {
        id: 1,
        nums: 3,
        dates: [
            {
                rif: "2017-03-13T00:00:02.000Z", 
                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'
                        }
                    }
                ]
            },
            {
                rif: "2017-03-13T00:00:03.000Z",
                date: '21-10-2012',
                promo: {
                    id: 2,
                    name: 'promo 2'
                },
                hours: [
                    {
                        hour: '22:00',
                        vision: {
                            id: 2,
                            name: '3D'
                        }
                    }
                ]
            },
            {
                rif: "2017-03-13T00:00:02.000Z",
                date: '21-10-2013',
                promo: {
                    id: 2,
                    name: 'nome promo'
                },
                hours: [
                    {
                        hour: '27:00',
                        vision: {
                            id: 2,
                            name: '3D'
                        }
                    }
                ]
            }
        ]
    };

    let res = [];

    let dates = Array.from(new Set(obj.dates.map(x => x.date)));

dates.forEach(v => {
    res.push(
        obj.dates.filter(x => x.date === v)
        .sort((a, b) => new Date(a.rif).getTime() - new Date(b.rif).getTime())
        .reduce((acc, curr) => {

            let hours = acc.hours.concat(curr.hours);         
            let newHours = [];

            Array.from(new Set(hours.map(x => x.hour)))
            .forEach(h => {
                newHours.push(
                    hours.filter(x => x.hour === h)
                    .reduce((s, d) => Object.assign(s, d))
                )
            });

            return Object.assign(acc, curr, { hours: newHours });
        })
    );
});
        
console.log(res);

于 2017-07-02T12:15:03.910 回答