簡體   English   中英

將共享值的數組中的對象合並到二維子數組中

[英]Merge objects in an array with a shared value into a 2dimensional sub 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},
]

什么是最干凈,最有效的分組方式,以便將具有相同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);
}

但我覺得必須有一種更清潔的方法

您可以對數組進行排序並減少項目,並在相同的數組中收集相同的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; } 

這個怎么樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM