javascript split array into chunks
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 twolet input = [1,2,3,4,5,6,7,8,9];
let chunked = []
let size = 2;
for (let i = 0; i < input.length; i += size) {
chunked.push(input.slice(i, i + size))
}
console.log(chunked)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:
- turn text into links javascript
- how to add two attay into object in javascript
- datepicker in react
- length of array javascript
- node js callback
- javascript format date yyyy-mm-dd
- convert an image into Base64 string using JavaScript
- saveas angular 6
- passing data variable using ajax
- create range array javascript
- js extract image from mp4
- convert json to object jackson
- jquery check checkbox
- how to export module in node js
- redux connect
- display json data in html table using javascript dynamically
- JSX not allowed in files with extension '.js'
- javascript biggest number
- know if a mobile open the website js
- javascript code for find the last element in array
- in out time of nodes
- how to trim the file name when length more than 10 in angular
- javascript detect textarea change
- check multiple ifield if it's blank in jquery