Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 697 Bytes

aperture.md

File metadata and controls

25 lines (18 loc) · 697 Bytes
标题 标签
aperture(创建连续数组) array,intermediate(数组,两者之间的)

创建连续元素的n元组数组。

  • 使用Array.prototype.slice()Array.prototype.map()创建适当长度的数组。
  • 使用arr中连续元素的n元组填充数组。
  • 如果n大于arr的长度,则返回一个空数组。

代码如下:

    const aperture = (arr,n) => n > arr.length ? [] : arr.slice(n - 1).map((v,i) => arr.slice(i,i+n));

调用方式:

    aperture([1, 2, 3, 4],2); // [[1, 2], [2, 3], [3, 4]]
    aperture([1, 2, 3, 4],3); // [[1, 2, 3], [2, 3, 4]]
    aperture([1, 2, 3, 4],5); // []

应用场景