I am working with an array containing 100 elements. My goal is to extract elements from the 10th to the 15th position (10, 11, 12, 13, 14, 15), then from the 20th to the 25th, followed by the 30th to the 35th, and so on in increments of 4, storing them in a new 2D array format. I have managed to store the values in a 2D array, but I am struggling with creating the desired gap.
One approach I explored involved chunking the array into sets of 6 elements (as I always need the first six elements of each "decade").
var newArr = [];
while(arr.length) newArr.push(arr.splice(0,6));
Alternatively, I found a function that splits an array into parts:
function splitArray(array, part) {
var tmp = [];
for(var i = 0; i < array.length; i += part) {
tmp.push(array.slice(i, i + part));
}
return tmp;
}
console.log(splitArray(m1, 6));
However, before implementing this splitting logic, I need to figure out how to create a new array consisting of elements from positions 10-15, 20-25, and so forth. Can you provide guidance on how I can achieve this?