如何地图上列要素在JavaScript

0

的问题

只要素,有价值大于或等于门槛必须保持在该阵列。 然后,一个新的阵列将必须创建其中将包含多个对象。 每个这些目的将有两个属性、开始和结束。

如果有若干因素中的一个行(有时间戳,10分钟开),他们将分组在相同的对象。 在那里启动的价值将时间戳的第一个元素和结束的价值将时间戳值的最后一项要件的组加10分钟。

如果没有几个要素后,将开始的价值将时间戳和结束会的时间戳加上10分钟。

const data = [{
    timestamp: '2021-11-23T14:00:00+0000',
    amount: 21
  },
  {
    timestamp: '2021-11-23T14:10:00+0000',
    amount: 27
  },
  {
    timestamp: '2021-11-23T14:20:00+0000',
    amount: 31
  },
  {
    timestamp: '2021-11-23T14:30:00+0000',
    amount: 29
  },
  {
    timestamp: '2021-11-23T14:40:00+0000',
    amount: 18
  },
  {
    timestamp: '2021-11-23T14:50:00+0000',
    amount: 17
  },
  {
    timestamp: '2021-11-23T15:00:00+0000',
    amount: 25
  },
  {
    timestamp: '2021-11-23T15:10:00+0000',
    amount: 21
  }
]

const threshold = 25
const aboveThreshold = data.filter(element => element.amount >= threshold)
const workSchedule = []

for (let i = 0; i < aboveThreshold.length; i++) {
  if (i === 0) {
    workSchedule.push({
      start: aboveThreshold[i].timestamp,
      end: aboveThreshold[i + 1].timestamp
    })
  }
  if (i > 0 && i < aboveThreshold.length - 1) {
    if (aboveThreshold[i].timestamp.slice(11, 13) === aboveThreshold[i + 1].timestamp.slice(11, 13)) {
      workSchedule.push({
        start: aboveThreshold[i].timestamp,
        end: aboveThreshold[i + 1].timestamp
      })
    }
  }
  if (i === aboveThreshold.length - 1) {
    workSchedule.push({
      start: aboveThreshold[i].timestamp,
      end: aboveThreshold[i].timestamp
    })
  }
}

console.log(workSchedule)

但最终的结果是我想要的是如下:

[
    {
        start: '2021-11-23T14:10:00+0000',
        end: '2021-11-23T14:40:00+0000'
    },
    {
        start: '2021-11-23T15:00:00+0000',
        end: '2021-11-23T15:10:00+0000'
    }
]

我希望我是清楚的

arrays javascript typescript
2021-11-23 20:24:54
1

最好的答案

1

你可以申请一个简单的减少功能得到你想要的结果有一点点帮助 Date 对象。 这里是一个解决方案:

const aboveThreshold = data.filter(element => element.amount >= threshold);

const nws = aboveThreshold.reduce((acc, v) => {
  const end = new Date(Date.parse(v.timestamp) + 600000);
  if (acc.length === 0) return [{ start: v.timestamp, end: end.toISOString() }];
  let diff = Date.parse(v.timestamp) - Date.parse(acc[acc.length - 1].end);
  // checks if the difference is less than 10 minutes
  if (diff <= 10 * 60 * 1000) {
    acc[acc.length - 1].end = end.toISOString();
  } else {
    acc.push({ start: v.timestamp, end: end.toISOString() });
  }
  return acc
}, []);

检查出 减少文件.

这是结果,它给你的数据

[{
  end: "2021-11-23T14:40:00.000Z",
  start: "2021-11-23T14:10:00+0000"
}, {
  end: "2021-11-23T15:10:00.000Z",
  start: "2021-11-23T15:00:00+0000"
}]
2021-11-23 22:14:24

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................