I\'m looking for an array method implementation named Array.window(n)
that invoked on an array with parameter n
, would give a contiguous overlappin
Here you go with a slightly shorter example using array functions:
let a = [1, 2, 3, 4, 5, 6];
function windowedSlice(arr, size) {
let result = [];
arr.some((el, i) => {
if (i + size >= arr.length) return true;
result.push(arr.slice(i, i + size));
});
return result;
}
console.log(windowedSlice(a, 2));
console.log(windowedSlice(a, 3));
console.log(windowedSlice(a, 4));
console.log(windowedSlice(a, 5));