# Javascript Array chunkArrayInGroups(arr, size)

![](/files/SRvrZ4MXOQH6EkTFikS9)

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/javascript-array-chunkarrayingroups-arr-size.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
