Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yckart/1b1f8bf36e4acaa49bf6 to your computer and use it in GitHub Desktop.
Save yckart/1b1f8bf36e4acaa49bf6 to your computer and use it in GitHub Desktop.
A nice way to go through an array, step by step.
var array = [1, 2, 3, 4, 5, 6, 7, 8];
var iterator = 0;

console.log( array.slice(iterator, iterator += 1) ); // => [1]
console.log( array.slice(iterator, iterator += 2) ); // => [2, 3]
console.log( array.slice(iterator, iterator += 3) ); // => [4, 5, 6]

The usage, with the utility-function below, is quite simple:

var step = stepArray([1, 2, 3, 4, 5, 6, 7, 8]);

console.log( step(1) ); // => [1]
console.log( step(2) ); // => [2, 3]
console.log( step(3) ); // => [4, 5, 6]
var stepArray = function (array) {
var iterator = 0;
return function (index) {
return array.slice(iterator, iterator += index);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment