Let's say I have this array:
var arr = [1,2,3,4,5,6,7,8,9,10]
Example: I want to construct 5 arrays with each pair in it so it becomes,
arr1 = [1,2]
arr2 = [2,4]
arr3 = [5,6]
This can of course be solved with the modulo operator(%), since those are simply pairs - so it becomes:
for (var i = 0; i < arr.length; i++) {
if(arr[i] % 2 === 0)
window['arr' + i].push(arr[i], arr[i - 1])
}
There are other ways, e.g with nested loops etc.
I'm feeling that this can be solved with a simpler way however, so I'd like to see more suggestions
So what's an elegant way to loop every 'n' items in an array, perform some operation on them and then move on to the next 'n' elements.
Update:
The example above deals with 2 elements in a a 10-element array- That's purely random. I'm not looking for a way to deal with pairs in an array - The question is about how to loop every N elements in an array, perform whatever operation on those N elements and move on to the next N elements
I'm also not looking to create new arrays - The question has to do with iterating over the original array, only.