gpt4 book ai didi

javascript - 在日期属性中按天按数组重新分组

转载 作者:行者123 更新时间:2023-12-01 03:15:55 24 4
gpt4 key购买 nike

如何创建一个在日期属性中按天重新组合数组的函数

例如我有这个数组..

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_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'},
]
};

提前致谢! ..

最佳答案

以下内容应产生您正在寻找的所需输出:

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 regrouped = [];

for (var i = 0; i < _array.length; i++) {
var addNew = true;
for (var j = 0; j < regrouped.length; j++) {
if (_array[i].date === regrouped[j][0].date) {
regrouped[j].push(_array[i]);
addNew = false;
}
}
if (addNew) {
regrouped.push([_array[i]]);
}
}

console.log(regrouped);

关于javascript - 在日期属性中按天按数组重新分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537894/

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