Imagine we have an array like this:
let numbers = [1,2,3,4,5,6,7,8,9,10]
For example: If we want to create 5 sets with each pair of numbers in it, the result would look like this,
set1 = [1,2]
set2 = [2,4]
set3 = [5,6]
We can achieve this using the modulo operator(%), specifically because these are pairs - like so:
for (let i = 0; i < numbers.length; i++) {
if(numbers[i] % 2 === 0)
window['set' + i].push(numbers[i], numbers[i - 1])
}
There are various other methods, such as using nested loops.
I feel there might be a simpler solution out there, so I'd appreciate any suggestions!
Now, what's a more elegant way to iterate through every 'n' items in an array, apply a certain operation on them, and then proceed to the next 'n' elements?
Update:
The previous example focused on dealing with pairs from a 10-element array- That was just random. My aim isn't specifically about pairs - The main idea is how to loop through every N elements in an array, perform an operation, and then move on to the next N elements.
I'm not interested in generating new arrays - My query revolves around iterating over the original array exclusively.