简体   繁体   English

来自 arrays 数组的块数据

[英]Chunk data from array of arrays

Before marking this as answered by another question please note this is an array of arrays, not a flat array, also, the number I have given are an example, I have just shown them so you can visually see what is happening.在将其标记为由另一个问题回答之前,请注意这是一个 arrays 数组,而不是一个平面数组,此外,我给出的数字是一个示例,我刚刚展示了它们,以便您可以直观地看到正在发生的事情。

I am trying to loop through an array of arrays.我正在尝试遍历 arrays 数组。 I have the following array:-我有以下数组: -

[
   [1, 2, 3, 4, 5, 6, 7, 8, 9],
   [1, 2, 3, 4, 5, 6, 7, 8, 9],
   [1, 2, 3, 4, 5, 6, 7, 8, 9],
   [3, 2, 1, 6, 5, 4, 9, 8, 7],
   [6, 5, 4, 9, 8, 7, 3, 2, 1],
   [7, 8, 9, 3, 2, 1, 6, 5, 4]
]

How is it possible to transform this array into chunks of 3x3 for example:-如何将此数组转换为 3x3 的块,例如:-

[
   [1, 2, 3, 1, 2, 3, 1, 2, 3],
   [4, 5, 6, 4, 5, 6, 4, 5, 6],
   [7, 8, 9, 7, 8, 9, 7, 8, 9],
   [3, 2, 1, 6, 5, 4, 9, 8, 7],
   [6, 5, 4, 9, 8, 7, 3, 2, 1],
   [7, 8, 9, 3, 2, 1, 6, 5, 4],
]

As you can see from the array above I have chunked it using the first 3 values from each array and then by the 2nd 3n from each array and lastly the final 3n from each array.正如您从上面的数组中看到的那样,我使用每个数组的前 3 个值将其分块,然后使用每个数组的第二个 3n,最后是每个数组的最后一个 3n。

So the array would be chunked like the following:-所以数组将被分块如下: -

1 2 3 | 4 5 6 | 7 8 9
1 2 3 | 4 5 6 | 7 8 9
1 2 3 | 4 5 6 | 7 8 9
---------------------
3 2 1 | 6 5 4 | 9 8 7
6 5 4 | 9 8 7 | 3 2 1
7 8 9 | 3 2 1 | 6 5 4

I have tried to loop through each line and resetting the column count when it hits an increment and increasing the row but this didn't work.我试图遍历每一行并在达到增量并增加行时重置列数,但这不起作用。

I can update the question with previous attempts if this is of any help?如果这有帮助,我可以用以前的尝试更新问题? Also just a note, the array will be different sizes but always divisible by a particular number, for the above example I have chosen the number 3.另外请注意,数组将有不同的大小,但总是可以被特定数字整除,对于上面的示例,我选择了数字 3。

I have updated the question with more information.我已经用更多信息更新了这个问题。 The array of arrays will always be divisible by a specific number, this example shows a divisible number of 3. arrays 的数组总是能被一个特定的数字整除,这个例子显示了一个可整除的数字 3。

 const array = [ [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], ]; function chunk(array, chunk) { let result = []; for (let i = 0; i < array.length; i += chunk) { result.push(array.slice(i, i + chunk)); } return result; } const zip = (...rows) => [...rows[0]].map((_, c) => rows.map((row) => row[c])); const chunked = zip(...array.map((arr) => chunk(arr, 3))).map((arr) => arr.flat() ); console.log(chunked);

You can do it with a nested loop, slicing the array each time based on the size of the outer array.您可以使用嵌套循环来执行此操作,每次都根据外部数组的大小对数组进行切片。

 const arr = [ [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9] ]; let out = []; for(let i in arr) { out.push([]);} arr.forEach((e, i) => { let scale = e.length / arr.length; for(let j in arr) { out[j] = out[j].concat(e.slice(j * scale, j * scale + scale)); } }); console.log(out);

Here it is once more with your original array prior to your edit:在您编辑之前,这里再次使用您的原始数组:

 const arr = [ [1, 3, 2, 5, 2, 4, 3, 6, 8], [1, 4, 3, 6, 7, 3, 6, 4, 5], [2, 4, 1, 4, 6, 3, 7, 9, 7] ]; let out = []; for(let i in arr) { out.push([]);} arr.forEach((e, i) => { let scale = e.length / arr.length; for(let j in arr) { out[j] = out[j].concat(e.slice(j * scale, j * scale + scale)); } }); console.log(out);

Dynamic solution:动态解决方案:

 const arr = [ [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], ]; const columns = arr[0].length; const rows = arr.length; const result = []; for (let i = 0; i < arr.length; i++) { let count = 0; let prevElements = []; for (let j = 0; j < arr[i].length; j++) { prevElements.push(arr[i][j]); if ((j + 1) % (columns / rows) === 0) { result[count] = result[count] || []; result[count] = [...result[count], ...prevElements]; prevElements = []; count++; } } } console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM