Assuming you have the following array:
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},
]
what is the cleanest and most efficient way to group these such that each object with the same type_id
is placed in a sub array like this:
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}
]
I've tried wrestling with reduce to get this to work but so far I'm completely stumped.
edit: I do have this which works:
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);
}
but I feel like there must be a cleaner way