0

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);
}, {})
3
  • In my obj result example, "hours" makes override of only have the same hour. I think that i explained bad in my answer. Commented Jul 2, 2017 at 10:22
  • What does "distint" mean? Commented Jul 2, 2017 at 12:50
  • I fixed title :P Commented Jul 3, 2017 at 9:19

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.