Come mappare gli elementi dell'array in JavaScript

0

Domanda

Solo gli elementi che hanno un valore maggiore o uguale alla soglia deve essere conservato in un array. Poi un nuovo array dovrà essere creata con il quale conterrà diversi oggetti. Ognuno di questi oggetti hanno due proprietà, l'inizio e la fine.

Se ci sono diversi elementi in una riga (che hanno un timestamp a distanza di 10 minuti), saranno raggruppati in uno stesso oggetto. In cui il valore di partenza sarà il timestamp del primo elemento e il valore finale sarà il valore del timestamp dell'ultimo elemento del gruppo, oltre a 10 min.

Se non ci sono più elementi di seguito, il valore di partenza sarà il timestamp e la fine sarà il timestamp più di 10 minuti.

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)

Ma alla fine il risultato che cerco è la seguente:

[
    {
        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'
    }
]

Spero di essere stato chiaro

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

Migliore risposta

1

È possibile applicare un semplice ridurre la funzione per ottenere il risultato desiderato con un po ' di aiuto da Date oggetto. Ecco una soluzione:

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
}, []);

Check out Ridurre Documentazione.

Questo è il risultato dà con i tuoi dati

[{
  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

In altre lingue

Questa pagina è in altre lingue

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