Home > Mobile >  How to map array elements in JavaScript
How to map array elements in JavaScript

Time:11-24

Only elements that have a value greater than or equal to the threshold must be kept in the array. Then a new array will have to be created which will contain several objects. Each of these objects will have two properties, the start and the end.

If there are several elements in a row (which have a timestamp 10 minutes apart), they will be grouped in the same object. Where the start value will be the timestamp of the first element and the end value will be the timestamp value of the last element of the group plus 10 min.

If there are not several elements followed, the start value will be the timestamp and the end will be the timestamp plus 10 minutes.

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

But the end result I want is the following:

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

I hope I was clear

  • Related