Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active Aug 29, 2015
Embed
What would you like to do?
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