Dimasss,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<script>
function print(arr, before, after) {
var html = arr.reduce(function(html, el) {
return html + "<div>" + JSON.stringify(el) + "</div>";
}, before);
document.body.insertAdjacentHTML("beforeBegin", html + after);
}
function chunk(arr, size) {
var answer = [];
var temp = [];
for (var i = 0; i < arr.length; i++) {
temp.push(arr[i]);
if (temp.length === size) {
answer.push(temp);
temp = [];
}
}
if (temp.length > 0) {
answer.push(temp);
}
return answer;
}
var arr = chunk([1, 2, 3, 6, 5, 4, 7], 2);
print(arr, ".1 [", "]");
function chunk2(arr, size) {
arr = arr.slice(0);
for (var i = 0; i < arr.length; i++) {
arr[i] = arr.splice(i, size, arr[i]);
}
return arr;
}
var arr2 = chunk2([1, 2, 3, 6, 5, 4, 7], 2);
print(arr2, ".2 [", "]");
function chunk3(arr, size) {
return arr.map(function(el, i) {
return arr.splice(i, size, el);
});
}
var arr3 = chunk3([1, 2, 3, 6, 5, 4, 7], 2);
print(arr3, ".3 [", "]");
</script>
</body>
</html>