1

I have an array of objects that I am trying to group together based on similar dates. For example, this is the array of objects that I start with:

var array = [
      {
        startDate: "2018-11-23",
        name: "Enrollment",
        place: "room 2"

      },{
        startDate: "2018-11-23",
        name: "Breakfast",
        place: "break room"
      },{
        startDate: "2019-11-15",
        name: "Training",
        place: "room 10"

      }];

I've tried creating a for loop that iterates over each item in the array to see if the startDate matches any other startDates in my object but Im coming up short because of just a misunderstanding of how the for loop is supposed to iterate over an array and then assign an object within an object to keep 'name' and 'place' associated with each other.

What I need to have happen is a new object that contains any shared dates as the object's key with 'place' still associated to 'name'. This is my desired outcome from my starting array:

var output = [
      {
        startDate: "2018-11-23",
        event: {name: "Enrollment" 
                place: "room 2"}
        event2: {name: "Breakfast"
                place: "room  10"}
      },{
        startDate: "2019-11-15",
        name: "Training"
      }];
beznet
  • 532
  • 1
  • 5
  • 13
  • 3
    do you really want to have `event` with a number as key? why not take an array? btw, what have you tried? – Nina Scholz Jan 25 '19 at 20:12
  • Agreed with @NinaScholz better to use simple arrays instead of this nested object. It'll be hard to manipulate later. – Iskandar Reza Jan 25 '19 at 20:15
  • Agree, will be much better to use an array of events. Also, check your output, why last object don't have a place? why place of `event2` is not `break room`? – Shidersz Jan 25 '19 at 20:20

0 Answers0