Je construis une application avec Node.js, Express, Postgres et Sequelize.
J'obtiens une réponse qui ressemble à ceci:
{
2019-04-15: [
{ id: 101, type: 0 }, { id: 103, type: 0}
],
2019-04-17: [
{ id: 102, type: 4 }, { id: 104, type: 0}
]
}
Je veux regrouper tous les événements qui se produisent à la même date.
J'ai essayé
_.forEach(response, function(value) {
_.groupBy(value, value.bookings[0].date)
})
mais cela ne fonctionne pas.
Comment puis-je mapper et grouper un tableau?
Finalement, je veux avoir un objet (ou un tableau) qui ressemble à ceci:
[
{
"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"
}
]
}
]
4 Réponses :
Vous pouvez utiliser réduire
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)
Vous pouvez utiliser la fonction réduire pour regrouper les objets par date.
Cela suppose que le tableau n'a qu'un seul index.
.as-console-wrapper { max-height: 100% !important; top: 0; }
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);
Vous pouvez également l'utiliser pour accomplir ce que vous recherchez.
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);
})
Eh bien, je n'ai pas vu beaucoup de réponses mais depuis que je l'ai fait aussi
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);
Ces tableaux de
réservationspeuvent-ils avoir plus d'un seul objet contenant unedate?À ce stade, non. Les dates peuvent être
confirméesetproposées. Dans la requête, je ne renvoie que des datesconfirmées- toujours un seul objet.