Like if there are 20 elements in an array what is the quickest way to filter them out by tens?
Like for example elements, 0-9 and then 10-21?
Like if there are 20 elements in an array what is the quickest way to filter them out by tens?
Like for example elements, 0-9 and then 10-21?
I think, what you mean is splitting the array to chunks
each chunk with size equal to ten.
function chunkArray(array, size) {
let result = []
for (let i = 0; i < array.length; i += size) {
let chunk = array.slice(i, i + size)
result.push(chunk)
}
return result
}
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'];
let arrPairs = chunkArray(arr, 10);
console.log(arrPairs);
You can try:
array.reduce((accum, current) => {
if (accum[accum.length - 1].length === 10) {
accum.push([]);
}
accum[accum.length - 1].push(current);
return accum;
},[[]])