a function to loop array indexes in groups of 6 in javascript

Here's an example function that takes an array and loops through its indexes in groups of 6:

index.tsx
function printGroupedIndexes(arr) {
  for (let i = 0; i < arr.length; i += 6) {
    const groupIndexes = arr.slice(i, i + 6).map((item, index) => i + index);
    console.log(groupIndexes);
  }
}
195 chars
7 lines

Here's how you can use the function:

index.tsx
const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
printGroupedIndexes(myArray);
98 chars
3 lines

The output of the above code will be:

index.tsx
[0, 1, 2, 3, 4, 5]
[6, 7, 8, 9, 10, 11]
[12, 13, 14]
53 chars
4 lines

This function divides the array indexes into groups of 6 using a for loop and the slice() method. Within each group, the map() method is used to transform the array of values (which are taken from the original array using slice()) into an array of indexes.

gistlibby LogSnag