Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries)
Setup JS var arr = [];
for (var i = 0 ; i< 1000 ; i++) {
arr.push (i);
}
function splitArrayReduce (items, subArrLength ) {
return items.reduce ((x, y, i ) => {
if (i % subArrLength === 0 ) {
x.push ([y]);
} else {
x.slice (-1 )[0 ].push (y);
}
return x;
}, []);
};
function splitArrayWhile1 (items, subArrLength ) {
var arr = [];
var asdf = null ;
do {
asdf = arr.length * subArrLength;
arr.push (items.slice (asdf, asdf + subArrLength));
} while (arr.slice (-1 )[0 ].length === subArrLength);
return arr;
};
function splitArrayWhile2 (items, subArrLength ) {
var arr = [];
var asdf = null ;
var end;
do {
asdf = arr.length * subArrLength;
end = asdf + subArrLength;
arr.push (items.slice (asdf, end));
} while (items.length > end);
return arr;
};
function doPaellaMadrilenya (items, subsections ) {
if (subsections <= 0 ) return ;
if (subsections >= items.length ) return [items];
return items.reduce ((a,b ) => {
let group = a[a.length - 1 ];
if (!group || group.length >= subsections) {
a.push ([b]);
} else {
group.push (b);
}
return a;
}, []);
}
function doPaellaRecursive (items, subArrLength, start = 0 , end = start + subArrLength ) {
const chorizo = items.slice (start, end);
return items.length > end
? [ chorizo, ...doPaellaRecursive (items, subArrLength, start + subArrLength) ]
: [ chorizo ];
}
function doPaellaValenciana (items, subsections ) {
var paella = [];
var n = 0 ;
while (n < arr.length +subsections) {
if (n < arr.length ) {
paella.push (arr.slice (n, n+subsections));
}
n += subsections;
}
return paella;
};
function splitArrayWhileManual (items, subArrLength ) {
const desiredLength = items.length / subArrLength;
const arr = [];
let arrLength = arr.length ;
while (arrLength < desiredLength) {
let start = arrLength * subArrLength;
let end = start + subArrLength;
const arr2 = arr[arrLength] = [];
while (start < end && items[start]) {
arr2[arr2.length ] = items[start];
start++;
}
arrLength = arr.length ;
};
return arr;
};
function splitArrayDoWhileManual (items, subArrLength ) {
const itemsLength = items.length ;
const arr = [];
let start = arr.length * subArrLength;
let end = start + subArrLength;
do {
start = arr.length * subArrLength;
end = start + subArrLength;
const arr2 = [];
while (start < end && items[start]) {
arr2[arr2.length ] = items[start];
start++;
}
arr[arr.length ] = arr2;
} while (itemsLength > end);
return arr;
};
function doPaellaValencianaWithRomero (items, subsections ) {
var paella = [];
var n = 0 ;
var n2 = 0 ;
while (n < arr.length +subsections) {
if (n < arr.length ) {
paella[n2] = arr.slice (n, n+subsections);
}
n += subsections;
n2 += 1 ;
}
return paella;
};
function splitArrayKvothe (array, num ) {
let result = [];
for (let i = 0 , l = array.length ; i < l; i += num) {
result.push ([array.slice (i, i + num)]);
}
return result;
};
function splitArraySplice (array, num ) {
var arrCopy = array.slice (0 );
let a = [];
while (arrCopy.length > 0 ) {
a.push (arrCopy.splice (0 , num));
}
return a;
};
Teardown JS
Test cases
Test #1 Title *
Async
Code * splitArrayReduce (arr, 5 );
Test #2 Title *
Async
Code * splitArrayWhile1 (arr, 5 );
Title *
Async
Code * splitArrayWhile2 (arr, 5 );
Title *
Async
Code * doPaellaMadrilenya (arr, 5 );
Title *
Async
Code * doPaellaRecursive (arr, 5 );
Title *
Async
Code * doPaellaValenciana (arr, 5 );
Title *
Async
Code * doPaellaValencianaWithRomero (arr, 5 );
Title *
Async
Code * splitArrayWhileManual (arr, 5 );
Title *
Async
Code * splitArrayDoWhileManual (arr, 5 );
Title *
Async
Code * splitArrayKvothe (arr, 5 );
Title *
Async
Code * splitArraySplice (arr, 5 );