0

I have a users list like the following:

[
    {
        user: a,
        email: a.com,
        events: []
    },
    {
        user: b,
        email: b.com
        events: []
    }       
]

And an events list:

[
    {
        user: a,
        start: 2020-01-01
        end: 2020-01-02
    },
    {
        user: a,
        start: 2020-02-01
        end: 2020-02-02
    },    
    {
        user: b,
        start: 2020-03-01
        end: 2020-03-02
    },     
]

And I'm trying to combine the list of events by user into a array on the original users object like so:

[
    {
        user: a,
        email: a.com,
        events: [
            {
                start: 2020-01-01
                end: 2020-01-02
            },
            {
                start: 2020-02-01
                end: 2020-02-02
            }
        ]
    },
    {
        user: b,
        email: b.com,
        events: [
            {
                start: 2020-03-01
                end: 2020-03-02
            }
        ]
    }       
]

My original intuition is to simply iterate over the events list by each user and where they match by user value, push a new object to the users.events array. However, I'm not a JS pro, and I'm sure there's a combination of assign/filter/map/reduce or lodash that streamlines this type of problem. Any help is greatly appreciated!

1 Answer 1

1

Please try this:

const userEvents = [
    {
        user: a,
        email: a.com,
        events: []
    },
    {
        user: b,
        email: b.com,
        events: []
    }       
]

const userStartEnds = [
    {
        user: a,
        start: 2020-01-01,
        end: 2020-01-02
    },
    {
        user: a,
        start: 2020-02-01,
        end: 2020-02-02
    },    
    {
        user: b,
        start: 2020-03-01,
        end: 2020-03-02
    },     
]

[
    {
        user: a,
        email: a.com,
        events: [
            {
                start: 2020-01-01,
                end: 2020-01-02
            },
            {
                start: 2020-02-01,
                end: 2020-02-02
            }
        ]
    },
    {
        user: b,
        email: b.com,
        events: [
            {
                start: 2020-03-01,
                end: 2020-03-02
            }
        ]
    }       
]

userEvents.map((userEvent) => ({
  ...userEvent,
  events: userStartEnds
    .filter(({ user }) => (userEvent.email === user.email))
    .map(({ start, end }) => ({ start, end }))
}))
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm! In the filter, I used .filter(user => userEvent.email === user.email) ... any reason for the ({ })?
Just edited filter, map implementation. Orignal one couldn't work properly as I didn't return JSON as ({ }). You can return JSON as ({ }) without function processing. This is kind of arrow function usage, and please check arrow function document if possible.

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.