757 B
757 B
title, tags, expertise, author, firstSeen, lastUpdated
| title | tags | expertise | author | firstSeen | lastUpdated |
|---|---|---|---|---|---|
| Consecutive element subarrays | array | intermediate | chalarangelo | 2020-05-13T13:25:33+03:00 | 2020-10-18T20:24:28+03:00 |
Creates an array of n-tuples of consecutive elements.
- Use
Array.prototype.slice()andArray.prototype.map()to create an array of appropriate length. - Populate the array with
n-tuples of consecutive elements fromarr. - If
nis greater than the length ofarr, return an empty array.
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]); // []