gpt4 book ai didi

Javascript对象按日、月、年分组

转载 作者:行者123 更新时间:2023-11-29 10:33:22 25 4
gpt4 key购买 nike

我在网上搜索了一些示例,但没有找到一个像样的工作并完成我需要的简单任务,也许你可以帮助我。

我有一个对象数组:

[
{
"date": "2015-01-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
},
{
"date": "2015-01-02T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": false,
"size": 1024
},
{
"date": "2015-02-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": true,
"isCompleted": false,
"size": 1024
},
{
"date": "2015-02-02T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
},
{
"date": "2016-04-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": true,
"isCompleted": false,
"size": 1024
},
{
"date": "2016-04-02T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
}
]

我正在寻找一个简单直接的解决方案来按年分组,然后按月分组,然后按以下方式按日分组

结果应该是我可以像下面这样迭代的东西:

[
{
"2015": [
{
"JAN": [
{
"01": {
"date": "2015-01-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
},
"02": {
"date": "2015-01-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
}
}
],
"FEB": [
{
"01": {
"date": "2015-02-01T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
},
"02": {
"date": "2015-02-02T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
}
}
]
},
],
"2016": [
{
"APR": [
{
"02": {
"date": "2016-04-02T12:00:00.000Z",
"photoUrl": "",
"inProgress": false,
"isCompleted": true,
"size": 1024
}
}
]
}
]
}
]

最终目的是通过带 Angular ng-repeat 在列表中遍历它。

如果有人有建议,我相信这段代码对其他人来说非常有用,因为没有示例可以在网络上的任何地方采用日期的平面数组并将其转换为年、月、日的对象。

非常感谢!

最佳答案

您可以只检查是否设置了年份,如果没有,则分配一个包含空对象的数组。然后继续处理月份,稍后将项目分配给它。

var data = [{ date: "2015-01-01T12:00:00.000Z", photoUrl: "", inProgress: false, isCompleted: true, size: 1024 }, { date: "2015-01-02T12:00:00.000Z", photoUrl: "", inProgress: false, isCompleted: false, size: 1024 }, { date: "2015-02-01T12:00:00.000Z", photoUrl: "", inProgress: true, isCompleted: false, size: 1024 }, { date: "2015-02-02T12:00:00.000Z", photoUrl: "", inProgress: false, isCompleted: true, size: 1024 }, { date: "2016-04-01T12:00:00.000Z", photoUrl: "", inProgress: true, isCompleted: false, size: 1024 }, { date: "2016-04-02T12:00:00.000Z", photoUrl: "", inProgress: false, isCompleted: true, size: 1024 }],
grouped = Object.create(null);

data.forEach(function (a) {
var year = a.date.slice(0, 4),
month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'][a.date.slice(5, 7) - 1],
day = a.date.slice(8, 10);

grouped[year] = grouped[year] || [{}];
grouped[year][0][month] = grouped[year][0][month] || [{}];
grouped[year][0][month][0][day] = a;
});

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

关于Javascript对象按日、月、年分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com