最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop 'n' elements in groups in each iteration - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Dec 13, 2019 at 14:25 nicholaswmin asked Dec 28, 2014 at 3:21 nicholaswminnicholaswmin 22.9k16 gold badges101 silver badges173 bronze badges 4
  • 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 Commented Dec 28, 2014 at 3: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 Commented Dec 28, 2014 at 3:25
  • There are good example of splitting arrays, in the answers to this question stackoverflow.com/questions/7273668/… – Abhishek Goyal Commented Dec 28, 2014 at 3: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 Commented Dec 28, 2014 at 3:33
Add a comment  | 

3 Answers 3

Reset to default 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]
}

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);
});

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.

发布评论

评论列表(0)

  1. 暂无评论