任何人都可以建议一种方法来按对象键对对象数组进行分组,然后根据 JavaScript 中的分组创建一个新的对象数组吗?
Can anyone suggest of a way to group an array of objects by an object key then create a new array of objects based on the grouping in JavaScript?
比如我有一个这样的数组。这里有相同日期和不同值的数组对象。
[
{
"date": "2020-12-31T18:30:00.000Z",
"value": 450
},
{
"date": "2020-12-31T18:30:00.000Z",
"value2": 362
},
{
"date": "2020-12-31T18:30:00.000Z",
"value3": 699
},
{
"date": "2021-03-01T18:30:00.000Z",
"value": 269
},
{
"date": "2021-03-01T18:30:00.000Z",
"value2": 450
},
{
"date": "2021-03-02T18:30:00.000Z",
"value3": 841
},
{
"date": "2021-04-03T18:30:00.000Z",
"value": 700
},
]
我想按如下所示对日期进行分组和合并。具有相同“日期”的不同值合并到一个数组对象中。
[
{
"date": "2020-12-31T18:30:00.000Z",
"value": 450,
"value2": 362,
"value3": 699
},
{
"date": "2021-03-01T18:30:00.000Z",
"value": 269,
"value2": 450
},
{
"date": "2021-03-02T18:30:00.000Z",
"value3": 841
},
{
"date": "2021-04-03T18:30:00.000Z",
"value": 700
}
]
高效地获得结果
const arr = [
{
date: "2020-12-31T18:30:00.000Z",
value: 450,
},
{
date: "2020-12-31T18:30:00.000Z",
value2: 362,
},
{
date: "2020-12-31T18:30:00.000Z",
value3: 699,
},
{
date: "2021-03-01T18:30:00.000Z",
value: 269,
},
{
date: "2021-03-01T18:30:00.000Z",
value2: 450,
},
{
date: "2021-03-02T18:30:00.000Z",
value3: 841,
},
{
date: "2021-04-03T18:30:00.000Z",
value: 700,
},
];
const dict = arr.reduce((acc, curr) => {
const { date, ...rest } = curr;
if (!acc.has(date)) {
acc.set(date, curr);
} else {
const o = acc.get(curr.date);
Object.entries(curr).forEach(([k, v]) => (o[k] = v));
}
return acc;
}, new Map());
const result = [...dict.values()];
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
比如我有一个这样的数组。这里有相同日期和不同值的数组对象。
[
{
"date": "2020-12-31T18:30:00.000Z",
"value": 450
},
{
"date": "2020-12-31T18:30:00.000Z",
"value2": 362
},
{
"date": "2020-12-31T18:30:00.000Z",
"value3": 699
},
{
"date": "2021-03-01T18:30:00.000Z",
"value": 269
},
{
"date": "2021-03-01T18:30:00.000Z",
"value2": 450
},
{
"date": "2021-03-02T18:30:00.000Z",
"value3": 841
},
{
"date": "2021-04-03T18:30:00.000Z",
"value": 700
},
]
我想按如下所示对日期进行分组和合并。具有相同“日期”的不同值合并到一个数组对象中。
[
{
"date": "2020-12-31T18:30:00.000Z",
"value": 450,
"value2": 362,
"value3": 699
},
{
"date": "2021-03-01T18:30:00.000Z",
"value": 269,
"value2": 450
},
{
"date": "2021-03-02T18:30:00.000Z",
"value3": 841
},
{
"date": "2021-04-03T18:30:00.000Z",
"value": 700
}
]
const arr = [
{
date: "2020-12-31T18:30:00.000Z",
value: 450,
},
{
date: "2020-12-31T18:30:00.000Z",
value2: 362,
},
{
date: "2020-12-31T18:30:00.000Z",
value3: 699,
},
{
date: "2021-03-01T18:30:00.000Z",
value: 269,
},
{
date: "2021-03-01T18:30:00.000Z",
value2: 450,
},
{
date: "2021-03-02T18:30:00.000Z",
value3: 841,
},
{
date: "2021-04-03T18:30:00.000Z",
value: 700,
},
];
const dict = arr.reduce((acc, curr) => {
const { date, ...rest } = curr;
if (!acc.has(date)) {
acc.set(date, curr);
} else {
const o = acc.get(curr.date);
Object.entries(curr).forEach(([k, v]) => (o[k] = v));
}
return acc;
}, new Map());
const result = [...dict.values()];
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}