我有一个数组
arr = [1,2,3,4,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
现在,我要检查数组中的值是否连续。
具体来说,我想要这个
First Check给出第一个和第二个元素是连续的,而下一个元素不是连续的,则算法必须从连续编号开始的地方返回第一个元素
喜欢
First Check will give 1
Second Check will give 6
and so on...
Run Code Online (Sandbox Code Playgroud)
请事先帮助
/**
* Given an array of number, group algebraic sequences with d=1
* [1,2,5,4,8,11,14,13,12] => [[1,2],[4,5],[8],[11,12,13,14]]
*/
import {reduce, last} from 'lodash/fp';
export const groupSequences = (array) => (
reduce((result, value, index, collection) => {
if (value - collection[index - 1] === 1) {
const group = last(result);
group.push(value);
} else {
result.push([value]);
}
return result;
}, [])(array)
);
Run Code Online (Sandbox Code Playgroud)