twig education

node v10.24.1
version: 2.0.1
endpointsharetweet
const {chunk} = require('lodash') function groupArrayElements_1(array, n) { return chunk(array, n-1); } function groupArrayElements_2(array, n) { let result = []; while (array.length) { result.push(array.splice(0, n-1)); } return result; } function groupArrayElements_3(array, n) { let result = []; for (let i = n; i > 0; i--) { result.push(array.splice(0, Math.ceil(array.length / i))); } return result; } console.log(groupArrayElements_1([1, 2, 3, 4, 5], 3)) console.log(groupArrayElements_2([1, 2, 3, 4, 5], 3)) console.log(groupArrayElements_3([1, 2, 3, 4, 5], 3))
Loading…

no comments

    sign in to comment