简体   繁体   English

Javascript循环遍历数组并返回结束连续值的对象

[英]Javascript loop through an array and return the object(s) that end a streak of values

I have an array of objects that looks like this:我有一个看起来像这样的对象数组:

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}
];

I want to create a function that loops through this array and filters/returns entries that end a (minimum) streak of three lower values (<10), and at the same time start a (minimum) streak of three higher values (10).我想创建一个函数,为此三个较低的值(<10)的一个(最小)条纹,并且通过该阵列和环路滤波器/返回条目同时启动3个更高的值的一个(最小)条纹(10) . This should also work the other way around (filter entries that end a streak of three higher values (10) and at the same time start a streak of three lower values.)这也应该工作周围的其他方法(即年底起更高的值(10的条纹过滤器条目),并同时启动三个较低值的连胜。)

In the example above, the desired output should be an array with the following two objects:在上面的示例中,所需的输出应该是一个包含以下两个对象的数组:

{timestamp: "2020-09-24T23:02:10.257Z", jam_factor: 10},
{timestamp: "2020-09-29T18:02:23.282Z", jam_factor: 8.39212}

If your array is not yet sorted by timestamp, then first do that:如果您的数组尚未按时间戳排序,请先执行以下操作:

segment.sort((a,b) => a.timestamp.localeCompare(b.timestamp));

Then, if I understand correctly you want to filter entries that end a streak of three lower values, and at the same time start a streak of three higher (10) values.然后,如果我理解正确的话,你要为此三个较低值的条纹过滤条目,在同一时间开始的高3(10)值的连胜。

After your edit to your question, the code now also detects the inverse (start of a streak of three lower values after three higher ones):在您对问题进行编辑后,代码现在还可以检测到反相(在三个较高值之后连续三个较低值的开始):

 function filter(segment) { let result = []; let count = 0; let prevIsTen, j; for (let i = 0; i < segment.length; i++) { let isTen = segment[i].jam_factor === 10; if (isTen === prevIsTen) { if (++count == 3 && j) result.push(segment[j]); } else { j = count < 3 ? 0 : i; count = 1; } prevIsTen = isTen; } 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(filter(segment));

Is this what you meant?这是你的意思吗? We would still like to see your own effort我们还是希望看到你自己的努力

 const arr = [ { tz:1, jamfactor:10 }, { tz:2, jamfactor:10 }, { tz:3, jamfactor:7 }, { tz:4, jamfactor:8 }, { tz:5, jamfactor:9 }, { tz:6, jamfactor:10 }, { tz:7, jamfactor:10 }, { tz:8, jamfactor:10 }, { tz:9, jamfactor:8 }, { tz:10, jamfactor:9 }, { tz:11, jamfactor:10 }, { tz:12, jamfactor:7 }, { tz:13, jamfactor:8 }, { tz:14, jamfactor:9 }, { tz:15, jamfactor:10 } ] let cnt = 0 const jf = arr.reduce((acc, item) => { if (cnt === 3 && item.jamfactor >= 10) { acc.push(item) cnt = 0 } else { if (item.jamfactor < 10) cnt++ else if (item.jamfactor >= 10) cnt = 0 } return acc; }, []) console.log(jf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM