Array Split in batches
JS
S
JavaScriptSplit in batches / chunks
1const splitInBatches = (data: any[], batchSize: number) => {
2 const batch = batchSize | 10;
3 const batches = [];
4 for (let i = 0, j = data.length; i < j; i += batch) {
5 batches.push(data.slice(i, i + batchSize));
6 }
7 return batches;
8};
9
10export { splitInBatches };
11
12// Only first X items of the array
13const first3Only = matches.length > 3 ? matches.slice(0, 3) : matches;
14
15Created on 12/31/2020