Menu

gitpiper

aperture javascript Code Snippet in 2024

arrayintermediate

Last Updated: 15 March 2024

Creates an array of n-tuples of consecutive elements.

  • Use Array.prototype.slice() and Array.prototype.map() to create an array of appropriate length.
  • Populate the array with n-tuples of consecutive elements from arr.
  • If n is greater than the length of arr, 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]); // []

javascript snippet similar to aperture For You in March 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️