---
description: >-
  http://www.java2s.com/ref/javascript/javascript-array-chunkarrayingroups-arr-size.html
---

# Javascript Array chunkArrayInGroups(arr, size)

![](https://2494213435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LYjAG13rAPdbArSfRyB%2Fuploads%2FU80LQz2IGUkKzLoK1S6H%2Fimage.png?alt=media\&token=4f2b9938-ba7c-46a2-8fc7-b11aa8d4c261)

D:\Tiah\working\MARUNO-21\test.html

```html
<!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>
	/*/*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()
	function chunkArrayInGroups(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

```html
<!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>
```
