Menu

gitpiper

mapConsecutive javascript Code Snippet in 2022

arrayintermediate

Last Updated: 26 November 2022

Maps each block of n consencutive elements using the given function, fn.

  • Use Array.prototype.slice() to get arr with n elements removed from the left.
  • Use Array.prototype.map() and Array.prototype.slice() to apply fn to each block of n consecutive elements in arr.
const mapConsecutive = (arr, n, fn) => arr.slice(n - 1).map((v, i) => fn(arr.slice(i, i + n)));
mapConsecutive([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, x => x.join('-')); // ['1-2-3', '2-3-4', '3-4-5', '4-5-6', '5-6-7', '6-7-8', '7-8-9', '8-9-10'];

javascript snippet similar to mapConsecutive For You in November 2022

Subscribe to our Newsletter

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

© 2022 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! ✌️