简体   繁体   中英

Merge array of objects with sort by date

I've this object:

{
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'
                }
            }
        ]
    }
]}

I want to merge object that have the same date value and of these override promo and hours. Below there is the result that i want:

{
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'
                }
            }
        ]
    }
]}

i tried to make something, but i don't know how exacly do:

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

Resolved! Hope it is useful for somebody!

 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM