javascript array split chunk
JavaScript
const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
// Examples
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3); // [[1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4); // [[1, 2, 3, 4], [5, 6, 7, 8]]let input = [1,2,3,4,5,6,7,8];
let chunked = []
let size = 2;
Array.from({length: Math.ceil(input.length / size)}, (val, i) => {
chunked.push(input.slice(i * size, i * size + size))
})
console.log(chunked);function splitArrayIntoChunksOfLen(arr, len) {
var chunks = [], i = 0, n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, i += len));
}
return chunks;
}
var alphabet=['a','b','c','d','e','f'];
var alphabetPairs=splitArrayIntoChunksOfLen(alphabet,2); //split into chunks of two//#Source https://bit.ly/2neWfJ2
const chunk = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
arr.slice(i * size, i * size + size)
);
console.log(chunk([1, 2, 3, 4, 5], 2));
Also in JavaScript:
- Title
- javascript all type of data
- Category
- JavaScript
- Title
- copy file javascript
- Category
- JavaScript
- Title
- Encoding and Decoding Base64 Strings in Node.js
- Category
- JavaScript
- Title
- append item to array javascript
- Category
- JavaScript
- Title
- generate thumbnail of pdf using pf js
- Category
- JavaScript
- Title
- how to use react memo hooks
- Category
- JavaScript
- Title
- how to import a json string from a file in typescript
- Category
- JavaScript
- Title
- import dropdown react
- Category
- JavaScript
- Title
- how to set height of material ui dialog react
- Category
- JavaScript
- Title
- how to extract values from array in javascript
- Category
- JavaScript
- Title
- how to make a text editor in html
- Category
- JavaScript
- Title
- .includes javascript
- Category
- JavaScript
- Title
- how to use local static images in svelte
- Category
- JavaScript
- Title
- how to create seeds in sequelise
- Category
- JavaScript
- Title
- flutter cache json
- Category
- JavaScript
- Title
- JavaScript append text to div
- Category
- JavaScript
- Title
- declare multiple variables javascript
- Category
- JavaScript
- Title
- adonisjs vscode debugger
- Category
- JavaScript
- Title
- initialize a map js
- Category
- JavaScript
- Title
- get url react
- Category
- JavaScript
- Title
- get all a tags javascript
- Category
- JavaScript
- Title
- how to make a div scrollable
- Category
- JavaScript
- Title
- angular routing url params
- Category
- JavaScript
- Title
- how to add array numbers in javascript
- Category
- JavaScript
- Title
- atom javascript es6 linter
- Category
- JavaScript
- Title
- how to delay redirect in javascript
- Category
- JavaScript
- Title
- javascript ascii to hex
- Category
- JavaScript
- Title
- check if object has method javascript
- Category
- JavaScript
- Title
- export apk react native
- Category
- JavaScript
- Title
- how to make a random number guessing game in javascript
- Category
- JavaScript
- Title
- Heroku H10-App Crashed Error
- Category
- JavaScript
- Title
- How to create a nested schema in apollo graphql server
- Category
- JavaScript
- Title
- change the focus to next in angular forms
- Category
- JavaScript
- Title
- box shadow javascript style change
- Category
- JavaScript
- Title
- angular observable subscribe and unsubscribe
- Category
- JavaScript
- Title
- get the index of object in array
- Category
- JavaScript
- Title
- is undefined javascript
- Category
- JavaScript
- Title
- bubble sort javascript
- Category
- JavaScript
- Title
- ionic react use yarn
- Category
- JavaScript
- Title
- dropzone add download button addedfile
- Category
- JavaScript
- Title
- codemirror get object from textarea
- Category
- JavaScript
- Title
- get unique id angular
- Category
- JavaScript
- Title
- how the concat function works javascript
- Category
- JavaScript
- Title
- how to get the end of an array javascript
- Category
- JavaScript
- Title
- flask get summernote text
- Category
- JavaScript
- Title
- dynamic loaded content can't find library jqury
- Category
- JavaScript
- Title
- fixed header on scroll vuejs
- Category
- JavaScript
- Title
- how to know if a number has a decimal number js
- Category
- JavaScript
- Title
- find least common multiple javascript
- Category
- JavaScript
- Title
- html decode javascript
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- email validation in javascript
- Category
- JavaScript
- Title
- axios delete request payload
- Category
- JavaScript
- Title
- ajax file upload jquery
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- javascript checkbox checked
- Category
- JavaScript
- Title
- how to get the size of the window in javascript
- Category
- JavaScript
- Title
- angularjs cdn
- Category
- JavaScript
- Title
- how do i backspace from javascript calculator
- Category
- JavaScript
- Title
- delete all childs in node
- Category
- JavaScript
- Title
- create a json object in javascript
- Category
- JavaScript
- Title
- javascript check if string is json parsable
- Category
- JavaScript
- Title
- How do I check if an element is hidden in jQuery?
- Category
- JavaScript
- Title
- creating an object javascript
- Category
- JavaScript
- Title
- getelementbyid
- Category
- JavaScript
- Title
- how to run function after animation complete jquery
- Category
- JavaScript
- Title
- event listener in react
- Category
- JavaScript
- Title
- convert nuber into string react js
- Category
- JavaScript
- Title
- is object js
- Category
- JavaScript
- Title
- how to add an array to another array in javascript
- Category
- JavaScript
- Title
- javacript getHTTPURL
- Category
- JavaScript
- Title
- how to apply gavity to code in javascript
- Category
- JavaScript
- Title
- count value a to b character javascript
- Category
- JavaScript
- Title
- how to detect a keypress in javascript
- Category
- JavaScript
- Title
- how to make ajax request javascript
- Category
- JavaScript
- Title
- check if function exists javascript
- Category
- JavaScript
- Title
- dotenv
- Category
- JavaScript
- Title
- going through every attributes of an object javascript
- Category
- JavaScript
- Title
- format date javascript
- Category
- JavaScript
- Title
- convert json to object jackson
- Category
- JavaScript
- Title
- get width of a dom element js
- Category
- JavaScript
- Title
- javascript check if variable is object
- Category
- JavaScript
- Title
- defer parsing of javascript avada
- Category
- JavaScript
- Title
- filter in react native
- Category
- JavaScript
- Title
- href back page javascript
- Category
- JavaScript
- Title
- how to show hide div in html javascript
- Category
- JavaScript
- Title
- javascript class click event
- Category
- JavaScript
- Title
- array contains case insensitive javascript
- Category
- JavaScript
- Title
- factorial javascript
- Category
- JavaScript
- Title
- .pop js
- Category
- JavaScript
- Title
- angular for loop
- Category
- JavaScript
- Title
- how to get unique values from array in javascript without duplicate value
- Category
- JavaScript
- Title
- html parser javascript
- Category
- JavaScript
- Title
- discord.js clear code
- Category
- JavaScript
- Title
- define a route with path paramters in nodejs
- Category
- JavaScript
- Title
- dotenv jest
- Category
- JavaScript
- Title
- adding cors in angular
- Category
- JavaScript
- Title
- chart js title
- Category
- JavaScript
- Title
- angularjs left xx characters
- Category
- JavaScript
- Title
- axios post with header
- Category
- JavaScript