0

I'm looking to find a way to split an array into a set number of chunks. I've seen multiple posts on here which split it into chunks with a specified length, but that's not what I'm after. For example, consider the following array:

var array = ['1','2','3','4','5','6']

I would like to easily be able to pass this to a function such as the following

splitArray(array, 2)

Which would result in the following:

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

Edit: As i mentioned, I wasnt to evenly split an array into a set number of chunks, not a number of chunks with a specified length as other questions ask

G. Hanson
  • 53
  • 1
  • 9
  • 1
    Does this help you ? http://stackoverflow.com/questions/8495687/split-array-into-chunks – dreamcrash Dec 06 '16 at 16:36
  • You should make an attempt to solve this on your own before posting the question. We're here to fix existing code, not provide you with it from scratch. – Tyler Roper Dec 06 '16 at 16:38
  • 1
    The main problem here isn't necessarily spiting the array into chunks, rather determining what size of chunks you want and how to approach possible leftovers or array length not conforming. – li x Dec 06 '16 at 16:38
  • No, that splits it into the amount if items. For example `[1,2,3,4,5,6,7,8,9,10]` gets split into 5 arrays of 2 items, I want to split it into 2 arrays of 5 items – G. Hanson Dec 06 '16 at 16:39
  • @Santi my apologies. I'm more looking for the logic of the idea so I can then build a function – G. Hanson Dec 06 '16 at 16:42
  • @lix thanks, this is the kind of thing I was after, just needed a hand with the logic side of things – G. Hanson Dec 06 '16 at 16:42
  • Agree with @santi , I just tumbled upon a question where someone is asking for help in her assignment. – Nikunj Sardhara Dec 06 '16 at 16:46
  • @G.Hanson which simply means you need to divide the length of the source array by 2 (or the number of chunks you want) to get chunk size before passing it into the chunking function. still a dupe. – Kevin B Dec 06 '16 at 17:01

0 Answers0