簡體   English   中英

將 arrays 分組為進一步的子 arrays

[英]Grouping arrays into further sub arrays

假設我有一個 arrays 數組,如下所示:

const input = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd']]

我想對其進行安排,以便將它們進一步分類為 arrays 組,每個組中的第一個元素指示它們的排序方式。 結果將如下所示:

const output = [[[1, 'aaa'], [1, 'bbb']], [[2, 'ccc'], [2, 'ddd']]]

第一組將包含所有 arrays ,其中第一個元素是 1,第二組將所有 2 作為第一個元素,依此類推。

這個問題的第二個解決方案可能是我能找到的最接近的東西: 將數組組分組到單獨的子數組組

但是,我的修改似乎不起作用,我想我誤解了.reduce()的功能。 我試過的:

const input = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd']];

const output = input.reduce(
  (r, c, i, a) => (
    c[0] != a[i - 1][0] ? r.push([c]) : r[r.length - 1].push(c), r
  ), []);

我是在朝着正確的方向前進,還是在其他地方有更簡單的解決方案?

所以基本上我在這里(在.reduce()內部)正在做的是搜索一個包含一個值( [number, string] )的組,該值(id)等於我想要的值的數量添加。 如果我找到一個組,我會在該組內推送新值( nv ),否則我創建一個新組( [nv]

const input = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd'], [4, 'aaa'], [5, 'aaa']]

const output = input.reduce((groups, nv) => {
    const group = groups.find(g => g.some(([id]) => id === nv[0]))
    
    if(!group) groups.push([nv])
    else group.push(nv)

    return groups
}, [])

// output: [[[1, 'aaa'], [1, 'bbb']], [[2, 'ccc'], [2, 'ddd']], [[4, 'aaa']], [[5, 'aaa']]]

這種算法可能是可讀的。 我使用 curIndex 變量來跟蹤如何推送 arrays

let curIndex = 0;
const output = input.reduce((prev: any, cur) => {
  // if prev is empty
  if (prev.length == 0) prev.push([cur]);
  // if the number equal the cur at current index
  else if (prev[curIndex][0][0] == cur[0]) prev[curIndex].push(cur);
  // create new array
  else {
    prev.push([cur]);
    curIndex++;
  }

  return prev;
}, []);

我希望它會有所幫助

假設有可能跳過索引,1,2...4,5 我在這里使用了filter來使減少 function 盡可能簡單。

const input = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd'], [4, 'aaa'], [5, 'aaa']]

const output = input.reduce((acc, curr) => {
    const pos = curr[0] - 1
    if (!acc[pos]) acc[pos] = []
    return acc[pos].push(curr) && acc
}, []).filter(Boolean)

使用 object 來保存此信息。 鍵是嵌套數組的第一個元素; 該值(最初)是一個空數組,您可以將那些嵌套的 arrays 推入。

使用一個簡單的循環來實現這一點,最后只返回Object.values

 const input = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd'], [1, 'ccc']]; function nest(arr) { const out = {}; for (const [index, ...rest] of arr) { out[index]??= []; out[index].push([ index, ...rest ]); } return Object.values(out); } console.log(JSON.stringify(nest(input)));

附加文件

只需在 object 的幫助下進行分組。

 const data = [[1, 'aaa'], [1, 'bbb'], [2, 'ccc'], [2, 'ddd']], result = Object.values(data.reduce((r, a) => { (r[a[0]]??= []).push(a); return r; }, {})); console.log(result)
 .as-console-wrapper { max-height: 100%;important: top; 0; }

只需將每個數組項放入對應的數組索引中:

const groups = [];
input.forEach(
  item => {
    groups[item[0]-1] ??= [];
    groups[item[0]-1].push(item);
  }
);

如果缺少數字,這種方法可能會導致空數組項。 此外,它假設最小數字是 1。如果您不喜歡這些約束,您可以使用 object 代替:

const groupsObject = {};
input.forEach(
  item => {
    groupsObject[item[0]] ??= [];
    groupsObject[item[0]].push(item);
  }
);
const groups = Object.values(groupsObject);

暫無
暫無

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

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