data_chunk_array
v2.0.0
Published
Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements
Downloads
5
Maintainers
Readme
DataChunkArray
This function creates an array of elements split into groups of the given size. If the input array cannot be split evenly, the final chunk will contain the remaining elements.
Usage
const dataChunkArray = require('./dataChunkArray');
const inputArray = ['a', 'b', 'c', 'd'];
const chunkSize = 2;
const output = dataChunkArray(inputArray, chunkSize);
console.log(output);
Parameters
array
(Array): The input array to be chunked.
size
(number): The size of each chunk.
Return Value
(Array): An array of chunks, where each chunk contains size number of elements from the input array. If the input array cannot be split evenly, the final chunk will contain the remaining elements.
The splice() method is used to extract elements from the original array (arr) starting from index 0 up to the given size. This creates a chunk of elements that is pushed into the result array. The splice() method also removes the extracted elements from the original array. This process continues until the original array is empty.