Javascript 如何映射和分组对象数组
我正在用Node.js、Express、Postgres和Sequelize构建一个应用程序 我得到的答复如下:Javascript 如何映射和分组对象数组,javascript,node.js,lodash,Javascript,Node.js,Lodash,我正在用Node.js、Express、Postgres和Sequelize构建一个应用程序 我得到的答复如下: [ { "id": 101, "type": 0, "bookings": [ { "date": "2019-04-15T02:00:00.000Z" } ] }, { "id": 102,
[
{
"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"
}
]
}
]
{
2019-04-15: [
{ id: 101, type: 0 }, { id: 103, type: 0}
],
2019-04-17: [
{ id: 102, type: 4 }, { id: 104, type: 0}
]
}
我想把同一天发生的所有事件分组
我试过了
_.forEach(response, function(value) {
_.groupBy(value, value.bookings[0].date)
})
但它不起作用
如何映射和分组阵列
最后,我希望有一个对象(或数组)看起来像这样:
[
{
"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"
}
]
}
]
{
2019-04-15: [
{ id: 101, type: 0 }, { id: 103, type: 0}
],
2019-04-17: [
{ id: 102, type: 4 }, { id: 104, type: 0}
]
}
你可以用
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”}]
让op=data.reduce((op,{bookings,…rest})=>{
let key=bookings[0]。date.split('T',1)[0]
op[键]=op[键]| |[]
操作[键]。按下(休息)
返回操作
},{})
console.log(op)
您可以使用函数reduce
按日期对对象进行分组
这是假设数组只有一个索引
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,“类型”:0,“预订”:[{“日期”:“2019-04-15T02:00:00.000Z”},{“id”:104,“类型”:0,“预订”:[{“日期”:“2019-04-17T02:00:00.000Z”}];
让result=arr.reduce((a,{id,type,bookings:[{date}]})=>{
让key=date.substring(0,10);
(a[key]| |(a[key]=[])。push({id,type});
返回a;
},Object.create(null));
控制台日志(结果)代码>
.as console wrapper{max height:100%!important;top:0;}
您也可以使用它来完成所需的内容
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);
})
嗯,我没有看到那么多答案,但因为我也看到了
常数数据=[
{
“id”:101,
“类型”:0,
“预订”:[
{
“日期”:“2019-04-15T02:00:00.000Z”
}
]
},
{
“id”:102,
“类型”:4,
“预订”:[
{
“日期”:“2019-04-17T02:00:00.000Z”
}
]
},
{
“id”:103,
“类型”:0,
“预订”:[
{
“日期”:“2019-04-15T02:00:00.000Z”
}
]
},
{
“id”:104,
“类型”:0,
“预订”:[
{
“日期”:“2019-04-17T02:00:00.000Z”
}
]
}
];
const bookingsByDate={};
data.forEach((预订)=>{
booking.bookings.forEach((bookingDate)=>{
const 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=年+'-'+月+'-'+日;
如果(!bookingsByDate[fullDate]){
bookingsByDate[fullDate]=[];
}
bookingsByDate[完整日期]=[
…bookingsByDate[完整日期],
{
id:booking.id,
type:booking.type,
}
]
});
})
控制台日志(bookingsByDate);
那些预订
数组是否可以包含多个包含对象的日期
?此时,日期可以确认
和建议
。在查询中,我只返回已确认的日期-始终只返回一个对象。