我在网上搜索,但是在某些例子中,找不到合适的工作并执行我需要的简单任务,也许你可以帮助我。
我有一个对象数组:
for (int j = 0; j < blocks.Count; j++)
{
if (blocks[j].alive == false)
{
blocks.RemoveAt(j);
break;
}
}
for (int i = 0; i < blocks.Count; i++)
{
if (blocks[i].alive == false)
{
blocks.RemoveAt(i);
break;
}
}
我正在寻找一个简单的直接解决方案,按年分组,然后按月分组,然后按照以下方式分组
结果应该是我可以迭代的东西,如下所示:
[
{
"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
}
]
最终目的是通过带有角度的ng-repeat在列表中检查它。
如果有人有任何建议我确定此代码对其他代码非常有用,因为没有关于采用平面日期数组并将其转换为年,月,日对象的示例,净。
非常感谢!
答案 0 :(得分:2)
您可以检查是否设置了年份,如果没有,则指定一个包含空对象的数组。然后继续月份,然后将项目分配给它。
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; }
答案 1 :(得分:1)
Unfortunately, there doesn't seem to be a way to reduce this further as each level has different key/value requirements.
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 }];
var months = [,"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var re = /^(\d{4})-(\d{2})-(\d{2})/;
var results = data.reduce(function(results,value){
var date = value.date.match(re);
var temp = results;
var key = date[1];
if(!temp.hasOwnProperty(key)) temp[key] = {};
temp = temp[key];
key = months[date[2]|0];
if(!temp.hasOwnProperty(key)) temp[key] = {};
temp = temp[key];
key = date[3];
if(!temp.hasOwnProperty(key)) temp[key] = [];
temp = temp[key];
temp.push(value);
return results;
},{});
console.log(results);
.as-console-wrapper { max-height: 100% !important; top: 0; }