Skip to content

saurabhpedia/chunkArray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published