如何在日期属性
中创建按日重组数组的函数例如我有这个数组..
var _array = [
{name:"sample1" ,date:'08-07-2017'},
{name:"sample2" ,date:'08-07-2017'},
{name:"sample3" ,date:'08-08-2017'},
{name:"sample4" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
];
然后我调用函数regroup
regroup_byday(_array);
预期产出
{
[
{name:"sample1" ,date:'08-07-2017'},
{name:"sample2" ,date:'08-07-2017'},
],
[
{name:"sample3" ,date:'08-08-2017'},
],
[
{name:"sample4" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
]
};
提前谢谢! ..
答案 0 :(得分:1)
以下内容应产生您正在寻找的所需输出:
var _array = [
{name:"sample1" ,date:'08-07-2017'},
{name:"sample2" ,date:'08-07-2017'},
{name:"sample3" ,date:'08-08-2017'},
{name:"sample4" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
{name:"sample5" ,date:'08-09-2017'},
];
var result = Object.values(
_array.reduce((regroup, obj) => {
if(regroup[obj.date] === undefined) {
regroup[obj.date] = [obj];
} else {
regroup[obj.date].push(obj);
}
return regroup;
}, {})
);
console.log(result);
答案 1 :(得分:1)
循环遍历数组并将结果推送到中间的 Object ,并以日期为关键字, Object 的值将是您所需要的:
Capture<Class<?>>