<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <script>/*/*from w ww. j a v a 2 s .c o m*/// Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.// Remember to use Read-Search-Ask if you get stuck. Write your own code.// Here are some helpful links:// Array.prototype.push()// Array.prototype.slice()functionchunkArrayInGroups(arr, size) {var newArray = [];for (var i =0; i <arr.length; i += size) {newArray.push(arr.slice(i, size + i)); } return newArray; }console.log(chunkArrayInGroups(["a","b","c","d","e"],2)); </script></body></html>
Hoặc
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function chunkArrayInGroups(arr, size) {
// Break it up.
var newArr = [];
i = 0; /*from www .j a va2s .c om*/
while (arr.length > size) {
// grab first "size" elements from array
newArr[i] = arr.slice(0, size);
// remove first "size" elements from array
for (j = 0; j < size; j++) {
arr.shift();
}
i += 1;
}
newArr[i] = arr;
return newArr;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d","e"], 2));
</script>
</body>
</html>