# Generate all consecutive element subarrays from a JavaScript array

Getting a single element from an array is easy, but what if you want to get all possible `n`-tuples of consecutive elements from an array? This is a fairly uncommon problem, yet it can be solved with a simple function.

## Arrays of consecutive elements

All you need to do is use `Array.prototype.map()` in order to map each element to an array of `n` consecutive elements, using `Array.prototype.slice()` to extract the subarray. In order to make sure no subarrays with fewer than `n` elements are returned, you can use `Array.prototype.slice()` to remove the first `n - 1` elements.

Finally, you can use `Array.prototype.length` to check if the length of the array is greater than `n` and return an empty array if it is not.

```const aperture = (n, arr) =>
n > arr.length
? []
: arr.slice(n - 1).map((v, i) => arr.slice(i, i + n));

aperture(2, [1, 2, 3, 4]); // [[1, 2], [2, 3], [3, 4]]
aperture(3, [1, 2, 3, 4]); // [[1, 2, 3], [2, 3, 4]]
aperture(5, [1, 2, 3, 4]); // []```

## Mapped arrays of consecutive elements

Subsequently, if you want to apply a function to each of the subarrays, you can simply apply the function to each subarray during the mapping process.

```const apertureMap = (arr, n, fn) =>
n > arr.length
? []
arr.slice(n - 1).map((v, i) => fn(arr.slice(i, i + n)));

apertureMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, x => x.join('-'));
// ['1-2-3', '2-3-4', '3-4-5', '4-5-6', '5-6-7', '6-7-8', '7-8-9', '8-9-10'];```

## More like this

• JavaScript ·

### Calculate all permutations of a JavaScript array or string

Generate all permutations of an array's elements or a string's characters using recursion.

• JavaScript ·

### Remove matching elements from a JavaScript array

Given a predicate function, remove elements from an array that match the given condition.

• JavaScript ·

### Initialize a JavaScript array with a given numeric range

Learn how to create an inclusive array with numbers in a range, using a common `step` difference.

• JavaScript ·

### How can I find the insertion index of an element in a sorted JavaScript array?

Given a sorted array, find the correct index to insert a given value.

Start typing a keyphrase to see matching snippets.