Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
_.chunk
node v6.17.1
version:
master
endpoint
share
tweet
let slice = require('lodash/slice') // chunk function function chunk(array, size){ // #1 size = Math.max(size, 0); let length = array == null ? 0 : array.length; if((size < 1)||(!length)){ return []; } // #2 let index = 0; let resIndex = 0; let result = new Array(Math.ceil(length / size)); // #3 while(index < length){ result[resIndex++] = slice(array, index, (index+=size)); } return result; }
// test function chunk let t1 = chunk(['a', 'b', 'c', 'd'], 2) // => [['a', 'b'], ['c', 'd']]
let t2 = chunk(['a', 'b', 'c', 'd'], 3) console.log(t2); // => [['a', 'b', 'c'], ['d']]
Loading…
no comments
sign in
to comment