learnbydoing
learnbydoing

Reputation: 499

How to map array elements in JavaScript

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)

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 😬 and is there a simpler and easier to understand/read approach than what I've done so far?

Upvotes: 1

Views: 131

Answers (1)

Farzin Farzanehnia
Farzin Farzanehnia

Reputation: 1060

You can apply a simple reduce function to get the result you want with a little bit of help from Date object. Here is a solution:

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 Reduce Documentation.

This is the result it gives with your data

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

Upvotes: 1

Related Questions