0

Let's say I have an array of many things. For example, let's go with number 1 to 100.

let array = Array(1 ... 100)

I need to split this array into multiple arrays, by length. For example, something like this:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ...]

Basically my one-dimensional array needs to become a two-dimensional array, I need get a "grid", so to speak. I've seen many questions about spitting an array in half etc, but haven't found anything to split an array into multiple pieces, by length.

I could loop over the array, remove the first x elements, see if there is anything left, etc, but that feels very clunky. I would expect that there is an easier way to split an array like this?

Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95

0 Answers0