9

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.

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • This seems like an odd requirement. Why are you trying to dynamically create separate variables? I would think that at least you would want to create an array of arrays? – JLRishe Dec 28 '14 at 03:24
  • Dynamically creating the new `arr` variables is just there for the code example - It has nothing to do with the question at hand – nicholaswmin Dec 28 '14 at 03:25
  • There are good example of splitting arrays, in the answers to this question http://stackoverflow.com/questions/7273668/how-to-split-a-long-array-into-smaller-arrays-with-javascript – Abhishek Goyal Dec 28 '14 at 03:33
  • what is the logical significance of a pair? There are 10 elements in the array, 5 pairs, and repeats in the pairs, so elements are unused. Is this purely random? – twinlakes Dec 28 '14 at 03:33

3 Answers3

11

Use plain old for loop, like this

var N = 3;
for (var i = 0; i < array.length; i += N) {
    // Do your work with array[i], array[i+1]...array[i+N-1]
}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
5

Just increment by n in your for loop:

for (var i = 0; i < arr.length; i += 2)
    console.log(arr[i], arr[i + 1]);

You can write a function to automate walking through an array by n for you:

function walk(arr, n, fn) {
    for (var i = 0; i < arr.length; i += n)
        fn(arr.slice(i, i + n));
}

walk([1,2,3,4,5,6], 2, function (segment) {
    console.log(segment);
});
Casey Chu
  • 25,069
  • 10
  • 40
  • 59
1

I'm feeling that this can be solved with a simpler way however

I think that more efficient would make a better sentiment here than simpler. And when it comes to efficiency, the best way to page through data is only when it is actually needed.

The example below is based on iter-ops library, and operator page:

import {pipe, page} from 'iter-ops';

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const i = pipe(arr, page(2)); //=> Iterable<number[]>

// iteration below will trigger the actual paging,
// one page at a time:

for (const a of i) {
    console.log(a); //=> [1, 2], [2, 4], [5, 6]
}

P.S. I'm the author if iter-ops.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138