js split array into smaller arrays
Array.prototype.chunk = function(n) {
if (!this.length) {
return [];
}
return [this.slice(0, n)].concat(this.slice(n).chunk(n));
};
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].chunk(5));Array.prototype.chunk = function(size) {
let result = [];
while(this.length) {
result.push(this.splice(0, size));
}
return result;
}
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(arr.chunk(2));
Also in JavaScript:
- last element of an array javascript
- angular pipe json error
- javascript array merge
- javascript get data-id attribute
- javascript convert to boolean
- vimeo playback speed
- square brackets vs curly brackets javascript
- golang get unknown json request data
- javascript how to get every element after the 1st in an array
- discord.js send message to specific channel
- jquery checkbox checked value
- difference between type and method in ajax
- DatabaseError [SequelizeDatabaseError]: relation does not exist
- Bind Json file into a Datagridview in windows form using C#
- how to place text at custom position on canvas in javascript
- redux get state without store
- how to change the color using js
- js array to json
- vue get height of element ref
- get height of div use js
- billie eilish
- angular firebase
- event listener to elements with class
- do you need a semicolon in javascript