_.chunk

node v6.17.1
version: master
endpointsharetweet
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