Home > OS >  How do I create new arrays of objects with objects from an existing array, based on values in the ob
How do I create new arrays of objects with objects from an existing array, based on values in the ob

Time:10-29

I am trying to create new arrays of objects from an array of objects called data, based on one of their values.

Each object is an event and has a startDateTime and an endDateTime value which is a string in the format of:

"startDateTime": "2022-10-28T21:01:11"

And

"endDateTime": "2022-10-29T21:01:11"

What I want is then is three arrays:

  • One with events that have passed. I was thinking with endDateTime before current day and time.
  • A second array with events that are currently ongoing. Where endDateTime is after current day and time.
  • And lastly a third array with events that have not yet started. Where startDateTime is after current day and time.

I have tried the following:

In order to get current day and time I used this:

var date = new Date();
var dateString =
    date.getUTCFullYear()   "-"  
    ("0"   (date.getUTCMonth() 1)).slice(-2)   "-"  
    ("0"   date.getUTCDate()).slice(-2)   "T"  
    ("0"   date.getUTCHours()).slice(-2)   ":"  
    ("0"   date.getUTCMinutes()).slice(-2)   ":"  
    ("0"   date.getUTCSeconds()).slice(-2);

console.log(dateString)
// 2022-10-28T09:00:00

The original array looks somewhat like this but with many other events:

const data = [
               {
                "name": "Dinner",
                "numOfAttending": 4,
                "startDateTime": "2018-04-28T19:00:00",
                "endDateTime": "2018-04-28T22:00:00"
               },
               {
                "name": "Studying",
                "numOfAttending": 1,
                "startDateTime": "2020-09-01T09:00:00",
                "endDateTime": "2023-06-10T15:00:00"
               },
               {
                "name": "Graduating!",
                "numOfAttending": 25,
                "startDateTime": "2023-06-11T09:00:00",
                "endDateTime": "2023-06-11T12:00:00"
               }
             ]

Then I made three empty arrays to populate:

  const eventsDone = [];       // This one should have the past dinner event in it.
  const eventsOngoing = [];    // This one should have current studying event in it.
  const eventsUpcoming = [];   // And this one should have the upcoming graduating event in it.

I have been trying to do this with a for loop, looping through the original array and if the conditions are met they will be pushed to respective array:

for (let i = 0; i < data.length; i  ) {
    if (data[i].endDateTime < dateString) {
       eventsDone.push(data[i]);
    } 
    else if (data[i].endDateTime > dateString) {
       eventsOngoing.push(data[i]);
    }
    else if (data[i].startDateTime > dateString) {
       eventsUpcoming.push(data[i]);
    }
  }

But with no luck.

Thanks in advance!

CodePudding user response:

This seems more precise to. First checking past, then future (you got that right). All the rest is ongoing.

const data = [{
    "name": "Dinner",
    "numOfAttending": 4,
    "startDateTime": "2018-04-28T19:00:00",
    "endDateTime": "2018-04-28T22:00:00"
  },
  {
    "name": "Studying",
    "numOfAttending": 1,
    "startDateTime": "2020-09-01T09:00:00",
    "endDateTime": "2023-06-10T15:00:00"
  },
  {
    "name": "Graduating!",
    "numOfAttending": 25,
    "startDateTime": "2023-06-11T09:00:00",
    "endDateTime": "2023-06-11T12:00:00"
  }
]

const eventsDone = []; // This one should have the past dinner event in it.
const eventsOngoing = []; // This one should have current studying event in it.
const eventsUpcoming = [];

var dateString = (new Date()).toISOString();
for (let i = 0; i < data.length; i  ) {
  if (data[i].endDateTime < dateString) {
    eventsDone.push(data[i]);
  } else if (data[i].startDateTime > dateString) {
    eventsUpcoming.push(data[i]);
  } else {
    eventsOngoing.push(data[i]);
  }
}

console.log(eventsDone)
console.log(eventsOngoing)
console.log(eventsUpcoming)

  • Related