Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1.12 KB

find-consecutive.md

File metadata and controls

36 lines (23 loc) · 1.12 KB
标题 标签
findConsecutive(查找所有连续元素的数组) array(数组)

查找所有连续元素的数组。

使用 Array.prototype.slice() 创建一个数组,从一开始就删除了 n - 1 个元素。 使用 Array.prototype.map() 和 Array.prototype.slice() 将每个元素映射到一个包含 n 个连续元素的数组。

代码如下:

const findConsecutive = (arr, n) =>
  arr.slice(n - 1).map((v, i) => arr.slice(i, i + n));

ts 代码如下:

调用方式:

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

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/find-consecutive.html"></iframe>