jsfiddle.net/z7yrep20 Since you want to manipulate the values, have to do it more imperatively - construct an array of arrays, creating a new array when the last one's length is greater than 4. Calculate the string "index" you need, and push it along with the value to the array