How do you split an array into array pairs in JavaScript? -
i want split array pairs of arrays
so var arr=[2,3,4,5,6,4,3,5,5]
would newarr =[[2,3],[4,5],[6,4],[3,5],[5]]
there's no pre-baked function that, here's simple solution:
var splitpairs = function(arr) { var pairs = []; (var i=0 ; i<arr.length ; i+=2) { if (arr[i+1] !== undefined) { pairs.push ([arr[i], arr[i+1]]); } else { pairs.push ([arr[i]]); } } return pairs; };
Comments
Post a Comment