Dev Helpers
← Back

Build a sliding window

Build a sliding window. A self-contained example using the JavaScript runtime.

Open this example in the interactive library

Source code

const xs = [1, 2, 3, 4, 5],
  size = 3;
console.log(
  xs.slice(0, 1 + xs.length - size).map((_, i) => xs.slice(i, i + size)),
);

Sample output

[ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ] ]