1

I'm building an application with Node.js, Express, Postgres and Sequelize.

I get a response that looks like this:

[
    {
        "id": 101,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 102,
        "type": 4,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 103,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 104,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    }
]

I want to group all the events that happen on the same date.

I tried

_.forEach(response, function(value) {
    _.groupBy(value, value.bookings[0].date)
})

but it doesn't work.

How can I map and group an array?

Eventually I want to have an object (or array) that looks something like this:

{
    2019-04-15: [
        { id: 101, type: 0 }, { id: 103, type: 0}
    ],
    2019-04-17: [
        { id: 102, type: 4 }, { id: 104, type: 0}
    ]
}
2
  • Can those bookings arrays have more than a single date-containing object? Commented Apr 12, 2019 at 1:51
  • At that point no. Dates can be confirmed and proposed. In the query I return only confirmed dates - always only one object. Commented Apr 12, 2019 at 1:56

4 Answers 4

4

You can use reduce

let data = [{"id": 101,"type": 0,"bookings": [{"date": "2019-04-15T02:00:00.000Z"}]},{"id": 102,"type": 4,"bookings": [{"date": "2019-04-17T02:00:00.000Z"}]},{"id": 103,"type": 0,"bookings": [{"date": "2019-04-15T02:00:00.000Z"}]},{"id": 104,"type": 0,"bookings": [{"date": "2019-04-17T02:00:00.000Z"}]}]

let op = data.reduce((op,{bookings,...rest}) => {
  let key = bookings[0].date.split('T',1)[0]
  op[key] = op[key] || []
  op[key].push(rest)
  return op
},{})

console.log(op)

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

Comments

0

You can use the function reduce for grouping the objects by date.

This is assuming the array has only one index.

let arr = [    {        "id": 101,        "type": 0,        "bookings": [            {                "date": "2019-04-15T02:00:00.000Z"            }        ]    },    {        "id": 102,        "type": 4,        "bookings": [            {                "date": "2019-04-17T02:00:00.000Z"            }        ]    },    {        "id": 103,        "type": 0,        "bookings": [            {                "date": "2019-04-15T02:00:00.000Z"            }        ]    },    {        "id": 104,        "type": 0,        "bookings": [            {                "date": "2019-04-17T02:00:00.000Z"            }        ]    }];

let result = arr.reduce((a, {id, type, bookings: [{date}]}) => {
  let key = date.substring(0, 10);
  (a[key] || (a[key] = [])).push({id, type});
  return a;
}, Object.create(null));


console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You can also use this to accomplish what you're looking for.

let response = [
    {
        "id": 101,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 102,
        "type": 4,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 103,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 104,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    }
]

let dateGroupings = {};

response.forEach((v)=> {
  let date = v.bookings[0].date.substring(0,10)
  if (!dateGroupings[date]){
    dateGroupings[date] = [];
  }

  let obj = { 
    id: v.id,
    type: v.type
  };

  dateGroupings[date].push(obj); 

})

Comments

0

Well i didn't see that many answers but since i did it too


const data = [
    {
        "id": 101,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 102,
        "type": 4,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 103,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-15T02:00:00.000Z"
            }
        ]
    },
    {
        "id": 104,
        "type": 0,
        "bookings": [
            {
                "date": "2019-04-17T02:00:00.000Z"
            }
        ]
    }
];

const bookingsByDate = {};

data.forEach((booking) => {    

    booking.bookings.forEach((bookingDate) => {
        const date = new Date(bookingDate.date);
        const day = date.getDate() < 10 ?  '0' + date.getDate(): date.getDate();
        const month = date.getMonth() < 10 ? '0' + date.getMonth(): date.getMonth();
        const year = date.getFullYear();

        const fullDate = year + '-' + month + '-' + day;

        if(!bookingsByDate[fullDate]) {
            bookingsByDate[fullDate] = [];
        }
        bookingsByDate[fullDate] = [
            ...bookingsByDate[fullDate],
            { 
                id: booking.id,
                type: booking.type,
            }
        ]
    });

})
console.log(bookingsByDate);

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.