日期多条件分组疑惑
原始数据
const arr = [
{
"id": 255,
"name": "上午班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-10"
},
{
"id": 256,
"name": "下午班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-10"
},
{
"id": 260,
"name": "晚班",
"user_id": 14,
"time_from": "2021-03-10", // 跨日期
"time_to": "2021-03-11" // 跨日期
},
{
"id": 261,
"name": "上午班",
"user_id": 14,
"time_from": "2021-03-11",
"time_to": "2021-03-11"
}
]
其中晚班 time_from
和time_to
是跨日期。
希望通过time_from
和time_to
进行分组。只要time_fro
m和time_to
有一个满足分组条件。就归到一类。
例如下面数据满足了time_from
为2021-03-10
和time_to为2021-03-11
,所以都归类到了2021-03-10
和2021-03-11
{
"id": 260,
"name": "晚班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-11"
}
最终得到如下格式
[
{
"date": "2021-03-10",
"list": [
{
"id": 255,
"name": "上午班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-10"
},
{
"id": 256,
"name": "下午班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-10"
},
{
"id": 260,
"name": "晚班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-11"
}
]
},
{
"date": "2021-03-11",
"list": [
{
"id": 260,
"name": "晚班",
"user_id": 14,
"time_from": "2021-03-10",
"time_to": "2021-03-11"
},
{
"id": 261,
"name": "上午班",
"user_id": 14,
"time_from": "2021-03-11",
"time_to": "2021-03-11"
}
]
}
]
如何转换
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)