按 javascript 中的日期对对象数组进行分组
Group array of objects by date in javascript
在 javascript
中按日期对对象数组进行分组
像这样的对象有3个
var Object1 = [
{
"data" : "1-1",
"createdAt": "2022-03-01",
},
{
"data" : "1-2",
"createdAt": "2022-03-02",
},
{
"data" : "1-4",
"createdAt": "2022-03-04",
},
{
"data" : "1-5",
"createdAt": "2022-03-05",
},
{
"data" : "1-6",
"createdAt": "2022-03-06",
},
{
"data" : "1-7",
"createdAt": "2022-03-07",
}
];
var Object2 = [
{
"data" : "2-1",
"createdAt": "2022-03-02",
},
{
"data" : "2-2",
"createdAt": "2022-03-03",
},
{
"data" : "2-3",
"createdAt": "2022-03-04",
},
{
"data" : "2-4",
"createdAt": "2022-03-05",
},
{
"data" : "2-5",
"createdAt": "2022-03-06",
},
{
"data" : "2-6",
"createdAt": "2022-03-07",
},
{
"data" : "2-7",
"createdAt": "2022-03-08",
}
];
var Object3 = [
{
"data" : "3-1",
"createdAt": "2022-03-03",
},
{
"data" : "3-2",
"createdAt": "2022-03-04",
},
{
"data" : "3-3",
"createdAt": "2022-03-05",
},
{
"data" : "3-4",
"createdAt": "2022-03-06",
},
{
"data" : "3-5",
"createdAt": "2022-03-07",
},
{
"data" : "3-6",
"createdAt": "2022-03-08",
},
{
"data" : "3-7",
"createdAt": "2022-03-09",
}
];
我想这样对它们进行分组:
var result = [
{
"Object1" : "1-1",
"Object2" : undefined or null,
"Object3" : undefined or null,
"createdAt": "2022-03-01",
},
{
"Object1" : "1-2",
"Object2" : "2-1",
"Object3" : undefined or null,
"createdAt": "2022-03-02",
},
{
"Object1" : undefined or null,
"Object2" : "2-2",
"Object3" : "3-1",
"createdAt": "2022-03-03",
},
{
"Object1" : "1-4",
"Object2" : "2-3",
"Object3" : "3-2",
"createdAt": "2022-03-04",
},
{
"Object1" : "1-5",
"Object2" : "2-4",
"Object3" : "3-3",
"createdAt": "2022-03-05",
},
{
"Object1" : "1-6",
"Object2" : "2-5",
"Object3" : "3-4",
"createdAt": "2022-03-06",
},
{
"Object1" : "1-7",
"Object2" : "2-6",
"Object3" : "3-5",
"createdAt": "2022-03-07",
},
{
"Object1" : undefined or null,
"Object2" : "2-7",
"Object3" : "3-6",
"createdAt": "2022-03-08",
},
{
"Object1" : undefined or null,
"Object2" : undefined or null,
"Object3" : "3-7",
"createdAt": "2022-03-09",
},
]
我想按日期对 n 个数组进行分组。
但是每个数组里面的日期也会不一样,不知道有没有数据。
中间可能日期是空的,也可能数据是空的。
如果时分秒数据混合,则不按日期分组。
我想我可以使用 foreach 循环,但我不知道我缺少什么。有人知道这个问题的答案吗?
这是一个相当标准的 'group-by' 但由于您有多个数组并且您希望将它们的变量名用作属性,因此您需要一种机制来执行此操作。
下面的示例首先使用组合对象的 shorthand property names, then iterates the Object.entries()
of this object to group by date assigning data by the variable names as keys. To ensure that all the objects have all the array properties even when they have no relevant entries it creates a template
object from the Object.keys()
从三个数组中创建一个对象,并在 group-by 操作中将其分布到每个累加器中。
const Object1 = [{ "data": "1-1", "createdAt": "2022-03-01", }, { "data": "1-2", "createdAt": "2022-03-02", }, { "data": "1-4", "createdAt": "2022-03-04", }, { "data": "1-5", "createdAt": "2022-03-05", }, { "data": "1-6", "createdAt": "2022-03-06", }, { "data": "1-7", "createdAt": "2022-03-07", }];
const Object2 = [{ "data": "2-1", "createdAt": "2022-03-02", }, { "data": "2-2", "createdAt": "2022-03-03", }, { "data": "2-3", "createdAt": "2022-03-04", }, { "data": "2-4", "createdAt": "2022-03-05", }, { "data": "2-5", "createdAt": "2022-03-06", }, { "data": "2-6", "createdAt": "2022-03-07", }, { "data": "2-7", "createdAt": "2022-03-08", }];
const Object3 = [{ "data": "3-1", "createdAt": "2022-03-03", }, { "data": "3-2", "createdAt": "2022-03-04", }, { "data": "3-3", "createdAt": "2022-03-05", }, { "data": "3-4", "createdAt": "2022-03-06", }, { "data": "3-5", "createdAt": "2022-03-07", }, { "data": "3-6", "createdAt": "2022-03-08", }, { "data": "3-7", "createdAt": "2022-03-09", }];
const objects = { Object1, Object2, Object3 };
const template = Object.fromEntries(Object.keys(objects).map(k => [k, undefined]));
const grouped = {};
for (const [objGroup, objArr] of Object.entries(objects)) {
for (const { createdAt, data } of objArr) {
const group = grouped[createdAt] ??= { ...template, createdAt };
group[objGroup] = data;
}
}
const result = Object.values(grouped);
console.log(result);
至于你那含糊不清的说法,'The date may be empty in the middle, or the data may be empty. If the hour, minute, and second data are mixed, they are not grouped by date',没有实际数据就没法补充了,但是你会需要稳定 属性 才能准确分组。
在 javascript
中按日期对对象数组进行分组像这样的对象有3个
var Object1 = [
{
"data" : "1-1",
"createdAt": "2022-03-01",
},
{
"data" : "1-2",
"createdAt": "2022-03-02",
},
{
"data" : "1-4",
"createdAt": "2022-03-04",
},
{
"data" : "1-5",
"createdAt": "2022-03-05",
},
{
"data" : "1-6",
"createdAt": "2022-03-06",
},
{
"data" : "1-7",
"createdAt": "2022-03-07",
}
];
var Object2 = [
{
"data" : "2-1",
"createdAt": "2022-03-02",
},
{
"data" : "2-2",
"createdAt": "2022-03-03",
},
{
"data" : "2-3",
"createdAt": "2022-03-04",
},
{
"data" : "2-4",
"createdAt": "2022-03-05",
},
{
"data" : "2-5",
"createdAt": "2022-03-06",
},
{
"data" : "2-6",
"createdAt": "2022-03-07",
},
{
"data" : "2-7",
"createdAt": "2022-03-08",
}
];
var Object3 = [
{
"data" : "3-1",
"createdAt": "2022-03-03",
},
{
"data" : "3-2",
"createdAt": "2022-03-04",
},
{
"data" : "3-3",
"createdAt": "2022-03-05",
},
{
"data" : "3-4",
"createdAt": "2022-03-06",
},
{
"data" : "3-5",
"createdAt": "2022-03-07",
},
{
"data" : "3-6",
"createdAt": "2022-03-08",
},
{
"data" : "3-7",
"createdAt": "2022-03-09",
}
];
我想这样对它们进行分组:
var result = [
{
"Object1" : "1-1",
"Object2" : undefined or null,
"Object3" : undefined or null,
"createdAt": "2022-03-01",
},
{
"Object1" : "1-2",
"Object2" : "2-1",
"Object3" : undefined or null,
"createdAt": "2022-03-02",
},
{
"Object1" : undefined or null,
"Object2" : "2-2",
"Object3" : "3-1",
"createdAt": "2022-03-03",
},
{
"Object1" : "1-4",
"Object2" : "2-3",
"Object3" : "3-2",
"createdAt": "2022-03-04",
},
{
"Object1" : "1-5",
"Object2" : "2-4",
"Object3" : "3-3",
"createdAt": "2022-03-05",
},
{
"Object1" : "1-6",
"Object2" : "2-5",
"Object3" : "3-4",
"createdAt": "2022-03-06",
},
{
"Object1" : "1-7",
"Object2" : "2-6",
"Object3" : "3-5",
"createdAt": "2022-03-07",
},
{
"Object1" : undefined or null,
"Object2" : "2-7",
"Object3" : "3-6",
"createdAt": "2022-03-08",
},
{
"Object1" : undefined or null,
"Object2" : undefined or null,
"Object3" : "3-7",
"createdAt": "2022-03-09",
},
]
我想按日期对 n 个数组进行分组。
但是每个数组里面的日期也会不一样,不知道有没有数据。 中间可能日期是空的,也可能数据是空的。 如果时分秒数据混合,则不按日期分组。
我想我可以使用 foreach 循环,但我不知道我缺少什么。有人知道这个问题的答案吗?
这是一个相当标准的 'group-by' 但由于您有多个数组并且您希望将它们的变量名用作属性,因此您需要一种机制来执行此操作。
下面的示例首先使用组合对象的 shorthand property names, then iterates the Object.entries()
of this object to group by date assigning data by the variable names as keys. To ensure that all the objects have all the array properties even when they have no relevant entries it creates a template
object from the Object.keys()
从三个数组中创建一个对象,并在 group-by 操作中将其分布到每个累加器中。
const Object1 = [{ "data": "1-1", "createdAt": "2022-03-01", }, { "data": "1-2", "createdAt": "2022-03-02", }, { "data": "1-4", "createdAt": "2022-03-04", }, { "data": "1-5", "createdAt": "2022-03-05", }, { "data": "1-6", "createdAt": "2022-03-06", }, { "data": "1-7", "createdAt": "2022-03-07", }];
const Object2 = [{ "data": "2-1", "createdAt": "2022-03-02", }, { "data": "2-2", "createdAt": "2022-03-03", }, { "data": "2-3", "createdAt": "2022-03-04", }, { "data": "2-4", "createdAt": "2022-03-05", }, { "data": "2-5", "createdAt": "2022-03-06", }, { "data": "2-6", "createdAt": "2022-03-07", }, { "data": "2-7", "createdAt": "2022-03-08", }];
const Object3 = [{ "data": "3-1", "createdAt": "2022-03-03", }, { "data": "3-2", "createdAt": "2022-03-04", }, { "data": "3-3", "createdAt": "2022-03-05", }, { "data": "3-4", "createdAt": "2022-03-06", }, { "data": "3-5", "createdAt": "2022-03-07", }, { "data": "3-6", "createdAt": "2022-03-08", }, { "data": "3-7", "createdAt": "2022-03-09", }];
const objects = { Object1, Object2, Object3 };
const template = Object.fromEntries(Object.keys(objects).map(k => [k, undefined]));
const grouped = {};
for (const [objGroup, objArr] of Object.entries(objects)) {
for (const { createdAt, data } of objArr) {
const group = grouped[createdAt] ??= { ...template, createdAt };
group[objGroup] = data;
}
}
const result = Object.values(grouped);
console.log(result);
至于你那含糊不清的说法,'The date may be empty in the middle, or the data may be empty. If the hour, minute, and second data are mixed, they are not grouped by date',没有实际数据就没法补充了,但是你会需要稳定 属性 才能准确分组。