gpt4 book ai didi

javascript - 将数组中具有共享值的对象合并到二维子数组中

转载 作者:行者123 更新时间:2023-12-01 01:25:06 26 4
gpt4 key购买 nike

假设您有以下数组:

const units = [
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 2, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 3, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 6, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 7, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 8, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 13, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 14, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 15, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 16, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 25, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 28, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 2, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 3, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 6, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 7, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 8, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 11, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 15, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 25, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 28, value: 0},
]

对这些进行分组的最干净、最有效的方法是什么,以便将具有相同 type_id 的每个对象放置在如下子数组中:

const groupedUnits = [
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 2, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 2, value: 0}
],
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 3, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 3, value: 0}
],
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 6, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 6, value: 0}
],
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 7, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 7, value: 0}
],
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 8, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 8, value: 0}
],
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 13, value: 0},
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 14, value: 0},
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 15, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 15, value: 0}
],
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 16, value: 0},
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 25, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 25, value: 0}
],
[
{unit_id: "33614036-6e8e-c06e-edf2-40f9aaf6224c", type_id: 28, value: 0},
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 28, value: 0}
],
{unit_id: "afc834b6-5a3b-09d1-d548-ff049dc931a9", type_id: 11, value: 0}
]

我尝试过使用reduce来让它工作,但到目前为止我完全被难住了。

编辑:我确实有这个有效:

const typeIds = lines
.map(u => u.type_id)
.reduce((types, currentType) => {
if (types.indexOf(currentType) == -1) {
return types.concat(currentType);
} else return types;
}, []);

const groupedUnits = [];

for (let type of typeIds) {
let tmpArr = lines.filter(u => u.type_id == type);
if (tmpArr.length == 1) {
groupedUnits.push(tmpArr[0]);
} else groupedUnits.push(tmpArr);
}

但我觉得必须有一种更干净的方法

最佳答案

这个怎么样:

const units = [
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 2, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 3, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 6, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 7, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 8, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 13, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 14, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 15, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 16, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 25, value: 0 },
{ unit_id: '33614036-6e8e-c06e-edf2-40f9aaf6224c', type_id: 28, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 2, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 3, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 6, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 7, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 8, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 11, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 15, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 25, value: 0 },
{ unit_id: 'afc834b6-5a3b-09d1-d548-ff049dc931a9', type_id: 28, value: 0 },
];

const groupedUnits = Object.values(units.reduce((a, b) => {
const arr = a[b.type_id] || [];
return {
...a,
[b.type_id]: [...arr, b],
};
}, {})).map(n => n.length > 1 ? n : n[0]);

关于javascript - 将数组中具有共享值的对象合并到二维子数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53876527/

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