分割一个数组来创建多维数组

时间:2021-11-03 21:27:03

I'm looking for a method to divide a one dimensional array by a number n to create a multidimensional array with each element having (at most) length n. For example

我正在寻找一种方法,将一维数组除以数字n,以创建一个多维数组,每个元素的长度(最多)为n

[1,2,3,4,5,6] / 3

should result in:

会导致:

[[1,2,3],[4,5,6]]

1 个解决方案

#1


5  

This should do it...

本应该做…

chunkedArr = arr.each_slice(3).to_a;

#1


5  

This should do it...

本应该做…

chunkedArr = arr.each_slice(3).to_a;