JavaScript遍历对象数组 筛选连续序列切换边界节点实现
双向序列切换点筛选函数实现
需求规则说明
需要遍历给定的拥堵系数数组,筛选符合两类切换条件的条目:
- 低转高切换点:当前条目是新高值(
jam_factor = 10)序列的起点,此前连续的低值(jam_factor < 10)序列长度≥3,新开启的高值序列长度也≥3 - 高转低切换点:当前条目是新低值序列的起点,此前连续的高值序列长度≥3,新开启的低值序列长度也≥3
给定示例数组的预期筛选结果为两个条目:
[ {timestamp: "2020-09-24T23:02:10.257Z", jam_factor: 10}, {timestamp: "2020-09-29T18:02:23.282Z", jam_factor: 8.39212} ]
实现思路
采用单次遍历分段的方式实现,无嵌套循环,时间复杂度O(n):
- 先把原数组中相邻的同类型(高/低值)条目合并为连续段,每个段记录段的起始条目、段内总长度
- 遍历相邻的段对,只要前一段长度≥3、后一段长度≥3,就把后一段的起始条目作为有效切换点加入结果数组
这种方式可以自动过滤掉序列中间的短抖动(比如连续高值中突然出现1个低值又切回高值的无效波动),不会误判。
完整可运行代码
function findValidSwitchPoints(segmentList) { // 长度不足时直接返回空,不可能凑出两个各3个元素的有效序列 if (segmentList.length < 6) return []; // 第一步:拆分连续同类型段 const segmentGroups = []; // 初始化第一段 let lastType = segmentList[0].jam_factor === 10 ? 'high' : 'low'; let currentGroup = { startItem: segmentList[0], length: 1 }; for (let i = 1; i < segmentList.length; i++) { const currentItem = segmentList[i]; const currentType = currentItem.jam_factor === 10 ? 'high' : 'low'; if (currentType === lastType) { currentGroup.length++; } else { // 遇到类型切换,存入当前段,开启新段 segmentGroups.push(currentGroup); lastType = currentType; currentGroup = { startItem: currentItem, length: 1 }; } } // 存入最后一个段 segmentGroups.push(currentGroup); // 第二步:筛选符合双段长度要求的切换点 const result = []; for (let i = 1; i < segmentGroups.length; i++) { const prevGroup = segmentGroups[i - 1]; const currGroup = segmentGroups[i]; if (prevGroup.length >= 3 && currGroup.length >= 3) { result.push(currGroup.startItem); } } return result; } // 测试用例 let segment = [ {timestamp: "2020-09-23T14:01:59.425Z", jam_factor: 5.12002}, {timestamp: "2020-09-24T20:02:20.656Z", jam_factor: 3.13285}, {timestamp: "2020-09-24T21:02:28.307Z", jam_factor: 2.84637}, {timestamp: "2020-09-24T22:04:09.648Z", jam_factor: 3.57281}, {timestamp: "2020-09-24T23:02:10.257Z", jam_factor: 10}, {timestamp: "2020-09-25T04:00:54.857Z", jam_factor: 10}, {timestamp: "2020-09-25T05:01:47.202Z", jam_factor: 10}, {timestamp: "2020-09-25T06:02:27.773Z", jam_factor: 10}, {timestamp: "2020-09-25T07:01:43.621Z", jam_factor: 10}, {timestamp: "2020-09-25T08:01:07.918Z", jam_factor: 10}, {timestamp: "2020-09-25T09:02:48.843Z", jam_factor: 5.2812}, {timestamp: "2020-09-25T10:02:44.509Z", jam_factor: 10}, {timestamp: "2020-09-25T11:01:47.002Z", jam_factor: 10}, {timestamp: "2020-09-25T12:02:46.718Z", jam_factor: 10}, {timestamp: "2020-09-25T13:00:32.164Z", jam_factor: 10}, {timestamp: "2020-09-28T05:02:44.037Z", jam_factor: 10}, {timestamp: "2020-09-28T14:01:57.837Z", jam_factor: 10}, {timestamp: "2020-09-29T11:01:34.661Z", jam_factor: 10}, {timestamp: "2020-09-29T17:02:20.367Z", jam_factor: 10}, {timestamp: "2020-09-29T18:02:23.282Z", jam_factor: 8.39212}, {timestamp: "2020-09-29T19:00:39.818Z", jam_factor: 7.38261}, {timestamp: "2020-09-30T03:01:49.720Z", jam_factor: 6.28182}, {timestamp: "2020-09-30T05:01:35.621Z", jam_factor: 5.98176}, {timestamp: "2020-09-30T06:00:27.023Z", jam_factor: 5.98176}, {timestamp: "2020-09-30T07:03:00.132Z", jam_factor: 4.15984} ]; // 运行后输出与预期完全一致 console.log(findValidSwitchPoints(segment));
可调整配置
代码中用于判定最短有效序列长度的数值3、高值判定阈值jam_factor === 10,都可以根据实际业务需求直接修改,不需要调整核心逻辑。
内容的提问来源于stack exchange,提问作者winecity
相关产品推荐
相关产品推荐

