0

I have two different array:

accomodation: [
  {
    id: 1,
    name: "Senator Hotel Fnideq",
    address: "Route de Ceuta, 93100 Fnidek, Morocco",
    checkin: "September 1",
    fullCheckinDate: "2021-09-01",
    checkout: "September 3",
    fullCheckoutDate: "2021-09-03",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  },
  {
    id: 2,
    name: "Kabek Morocco Hotel",
    address: "Omis Juy, 93100 Kabek, Morocco",
    checkin: "September 3",
    fullCheckinDate: "2021-09-03",
    checkout: "September 5",
    fullCheckoutDate: "2021-09-05",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  }
]
experiences: [
        {
            id: 1,
            fullDate: "2021-09-01",
            title: "Arrival",
            itinerary: // []
        },
        {
          id: 2,
          fullDate: "2021-09-02",
          title: "Sightseeing",
          itinerary: // []
        }
    ]

I want to find a way to combine the dates that are the same between the accommodation and the experiences into an object.

myTrips: [
  {
    accomodation: {
      id: 1,
      name: "Senator Hotel Fnideq",
      address: "Route de Ceuta, 93100 Fnidek, Morocco",
      checkin: "September 1",
      fullCheckinDate: "2021-09-01",
      checkout: "September 3",
      fullCheckoutDate: "2021-09-03",
      nights: 2,
      mealplan: "Breakfast,Lunch"
    },
    experiences: {
       id: 1,
       fullDate: "2021-09-01",
       title: "Arrival",
       itinerary: // []
    }
  },
  //... another object
]

I'm using dayjs to help with the dates. How do I go about this

Peoray
  • 1,705
  • 2
  • 12
  • 21
  • Typically refer to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Definitely not 01/09/2021, which is ambiguous. – jarmod Sep 02 '21 at 15:52
  • To approach this, you'd have to first format the dates from both arrays like @jarmod suggested and then merge the arrays into separate objects if the dates are the same. You can use reduce or lodash. Or you could sort the arrays by dates and merge them – Ovi Sep 02 '21 at 15:57
  • Some ideas for [merging objects by ID](https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id). You could potentially extend to include date overlaps. – jarmod Sep 02 '21 at 15:58
  • Not sure what you mean by the last statement. Can you show a code snippet? – Peoray Sep 02 '21 at 16:00
  • There's not really enough information here. Are the experience IDs related to the accommodation IDs, or are they independent? Can there be multiple experiences for a given stay at one hotel? Your proposed result, where experiences is a single object, suggests that there cannot be more than one yet it looks like both arrival and sightseeing are experiences while at the first hotel. – jarmod Sep 02 '21 at 16:06
  • Basically, I want to get the accommodation and experiences happening on the same day and have them in an object. The main thing are the dates, not ids. The ids have no relationship – Peoray Sep 02 '21 at 16:57

1 Answers1

0

const accomodation = [
  {
    id: 1,
    name: "Senator Hotel Fnideq",
    address: "Route de Ceuta, 93100 Fnidek, Morocco",
    checkin: "September 1",
    fullCheckinDate: "2021-09-01",
    checkout: "September 3",
    fullCheckoutDate: "2021-09-03",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  },
  {
    id: 2,
    name: "Kabek Morocco Hotel",
    address: "Omis Juy, 93100 Kabek, Morocco",
    checkin: "September 3",
    fullCheckinDate: "2021-09-03",
    checkout: "September 5",
    fullCheckoutDate: "2021-09-05",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  }
];

const experiences = [
    {
        id: 1,
        fullDate: "2021-09-01",
        title: "Arrival",
        itinerary: []
    },
    {
      id: 2,
      fullDate: "2021-09-02",
      title: "Sightseeing",
      itinerary: []
    }
];

const myTrips = [];
accomodation.map(acc => {
  const experience = experiences.filter(exp => {
  const date = new Date(exp.fullDate);
  return date >= new Date(acc.fullCheckinDate) && date <= new Date(acc.fullCheckoutDate);
});
  myTrips.push({accomodation: acc, experience});
});

console.log(myTrips);
  • I want the experiences that happens on 2021-09-01 should be together with accommodation that falls on the same date. See the myTrip array in my question – Peoray Sep 02 '21 at 16:56
  • The issue is that the accommodation has only check-in and checkout dates, I need a way to know the dates in-between – Peoray Sep 02 '21 at 16:58
  • This will only give me for one day, since we are staying in the same hotel for 2 days, I need to have the next day experience in the same object with the accommodation – Peoray Sep 02 '21 at 17:00