javascript - Efficiently break an array every X number of values? -
i want split array group of values starting @ end , leaving beginning remander array. here's example of goal:
arr = [0,1,2,3,4,5,6,7,8,9,10,11] interval = 5 #chop output = [[0,1],[2,3,4,5,6],[7,8,9,10,11]]
what's efficient way of doing this?
thanks help?
var arr = [0,1,2,3,4,5,6,7,8,9,10,11], interval = 5, output = []; while (arr.length >= interval) { output.unshift(arr.splice(-interval, interval)); } output.unshift(arr); console.log(output);
Comments
Post a Comment