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

时间:2018-12-20 21:50:42

标签: javascript

假设您具有以下数组:

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);
}

但是我觉得必须有一种更清洁的方式

2 个答案:

答案 0 :(得分:1)

您可以对数组进行排序并减少项目,并在相同的数组中收集相同的type_id

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 }],
    grouped = units
        .sort(({ type_id: a }, { type_id: b }) => a - b)
        .reduce((r, o) => {
            var i = r.length - 1;
            if (!r[i] || r[i].type_id !== o.type_id) {
                r.push(o);
            } else {
                if (!Array.isArray(r[i])) {
                    r[i] = [r[i]];
                }
                r[i].push(o);
            }
            return r;
        }, []);
        
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

没有排序,但采用内置排序方式来处理对象的正整数键。

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 }],
    grouped = Object.values(units.reduce((r, o) => {
        if (r[o.type_id]) {
            if (!Array.isArray(r[o.type_id])) {
                r[o.type_id] = [r[o.type_id]];
            }
            r[o.type_id].push(o);
        } else {
            r[o.type_id] = o;
        }
        return r;
    }, Object.create(null)));
        
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

如何?

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]);