Arrays of consecutive elements

Angelos Chalaris · JavaScript, Array · Apr 6, 2022

Finds all arrays of consecutive elements.

const findConsecutive = (arr, n) =>
  arr.slice(n - 1).map((v, i) => arr.slice(i, i + n));
findConsecutive([1, 2, 3, 4, 5], 2);
// [[1, 2], [2, 3], [3, 4], [4, 5]]

Recommended snippets