Solo los elementos que tienen un valor mayor o igual que el umbral deben mantenerse en la matriz. Luego, se deberá crear una nueva matriz que contendrá varios objetos. Cada uno de estos objetos tendrá dos propiedades, el inicio y el final.
Si hay varios elementos en una fila (que tienen una marca de tiempo de 10 minutos de diferencia), se agruparán en el mismo objeto. Donde el valor inicial será la marca de tiempo del primer elemento y el valor final será el valor de la marca de tiempo del último elemento del grupo más 10 min.
Si no hay varios elementos seguidos, el valor inicial será la marca de tiempo y el final será la marca de tiempo más 10 minutos.
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)Pero el resultado final que quiero es el siguiente:
[ { 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' } ]Espero haber sido claro 😬 y ¿hay un enfoque más simple y más fácil de entender/leer que lo que he hecho hasta ahora?
Puede aplicar una función de reducción simple para obtener el resultado que desea con un poco de ayuda del objeto Date . Aquí hay una solución:
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 }, []);Consulte Reducir documentación .
Este es el resultado que da con sus datos
[{ 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" }]