Ways to verify the timeframe between two specific dates

Having two distinctive arrays:

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: // []
        }
    ]

Looking to merge the common dates in accommodation and 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
]

Utilizing dayjs for date operations. What steps should I take?

Answer №1

const accommodations = [
  {
    id: 1,
    name: "Grand Hotel Riviera",
    address: "123 Ocean Avenue, Rivertown, USA",
    checkin: "September 15",
    fullCheckinDate: "2022-09-15",
    checkout: "September 20",
    fullCheckoutDate: "2022-09-20",
    nights: 5,
    mealplan: "Breakfast,Dinner"
  },
  {
    id: 2,
    name: "Sunset Resort Oasis",
    address: "456 Palm Beach Road, Sun City, USA",
    checkin: "September 20",
    fullCheckinDate: "2022-09-20",
    checkout: "September 22",
    fullCheckoutDate: "2022-09-22",
    nights: 2,
    mealplan: "All Inclusive"
  }
];

const activities = [
    {
        id: 1,
        fullDate: "2022-09-16",
        title: "Beach Day",
        itinerary: []
    },
    {
      id: 2,
      fullDate: "2022-09-18",
      title: "City Tour",
      itinerary: []
    }
];

const myVacation = [];
accommodations.map(acc => {
  const activity = activities.filter(act => {
  const date = new Date(act.fullDate);
  return date >= new Date(acc.fullCheckinDate) && date <= new Date(acc.fullCheckoutDate);
});
  myVacation.push({accommodation: acc, activity});
});

console.log(myVacation);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I keep receiving the same error (ENOENT) for all npm commands

Currently, I am running windows 8.1 x64 with all the latest updates installed. I encountered an error while using nodejs version 8.9.1 when running the command "npm -v". As a result, I decided to uninstall this version and switch to version 8.9.3. However ...

Axios transforms into a vow

The console.log outputs of the api and axios variables in the provided code are showing different results, with the state axios becoming a Promise even though no changes were made. The 'api' variable holds the normal axios instance while 'ax ...

graphic map and paintbrush

I've implemented an image map using shape circles, but now I'm looking for a way to draw visible circles or icons on the map. Is there a solution for this issue? Currently, I have used a canvas overlay on the image to draw on it This method wo ...

The function causes changes to an object parameter once it has been executed

I've encountered an issue with a function that is supposed to generate a string value from an object argument. When I call this function and then try to use the argument in another function, it seems to be getting changed somehow. Here is the code fo ...

What is the process for creating a three-dimensional Bezier curve using JavaScript, specifically with three.js and WebGL?

I have a good grasp on creating a simple bezier curve, but I am curious about how to transform the curve into 3D. Additionally, I am wondering if it is possible to plot multiple curves to form an image like a cone. Any advice or guidance on this matter wou ...

What is the best way to access an item within an item using HTML and AngularJS?

I attempted to retrieve a value from two dynamic objects using AngularJS. <div ng-controller="SampleController"> <div> {{item['111']['price']}} </div> within the SampleController $scope.item={111:{price:"232"},112:{ ...

Why is the state object empty in this React Native function?

One React-Native component I have is responsible for rendering an image gallery in a list format. This component is called once for each item on the parent list. It takes two props: "etiqueta," which contains the title of the item, and "galleries," which i ...

Why isn't the externally loaded JS file executing properly?

My javascript code functions properly when it's embedded within the HTML file. However, I encounter issues when I try to import it externally. The Google Developer Tools indicate that the file has been loaded successfully, but there seems to be no vis ...

Error: Unable to access 'map' property of an undefined variable in NextJS while using getStaticPaths

Currently facing an issue with dynamic routing in my Next.js project. I have a dynamic page [id].js and am trying to fetch data from an API. const res = await fetch(`myAPI`); const resData = await res.json(); const paths = resData.data.map((r) =&g ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

Experiencing issues with loading Vuex in Nativescript-Vue using the 'tns preview' command?

In the process of developing a trivia app for mobile, I have chosen to use Nativescript-Vue. When testing the app on the xcode iOS emulator, everything runs smoothly without any errors. However, when attempting to preview the app using the terminal command ...

What is the best way to make a drop down menu list appear when I click on the parent li, and then show the sub li using JavaScript?

I have a code that includes an image, HTML, and CSS code. Can anyone please tell me how I can set up a drop-down menu list to open the sub <li> elements when I click on a parent <li> using JavaScript? You can see the appearance of the code in t ...

Subject: Exploring the capabilities of the Vue-bootstrap-typeahead API serializer

Seeking guidance on a Vue application that utilizes vue-bootstrap-typeahead . I am puzzled by the usage of serializer in its API reference, especially when dealing with dropdown suggestions sourced from an external provider. Can someone elaborate on this c ...

Using Node.js to invoke an identifier loop

Having some trouble with this loop. Should I include variable c in the loop as well? Let me provide some context: there is a table and child(i) changes for every row. The SelectorT receives a sentence structured like this: Now\nIs\n09:00\n4 ...

Errors encountered while running `npm install discord.js`

I am currently facing an issue while trying to install discord.js. Unfortunately, I keep encountering the following errors: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <https://npm.co ...

Is there a way to determine the remaining time or percentage until document.ready is reached?

My usual approach is to display a loading animation like this: $('#load').show(); $(document).ready(function(){ $('#load').hide(); }); where the <div id="load"> contains just an animated gif. However, I've been conside ...

Using Selenium WebDriver to handle Angular requests in Java

I am currently developing tests for an angular-based application and I find myself in need of assistance. The specific task at hand involves creating a mechanism that will wait until all pending requests within the application have been processed before pr ...

Send both queries using JSON

I am trying to pass company information using Json, and I also want to pass the areas using the same method. However, I am encountering some issues. Can anyone provide assistance? if($_GET['method']=="get_all_companies"){ $query = "SELECT co ...

Create a form action dynamically with JavaScript

html code : <section class="main"> <form class="form-4" name="form4" id="form4" method="POST"> <p class="submit"> <button type="submit" name="submit" onclick="show()"><i class="icon-thumbs-up icon-large"></i> ...

Ways to change the visibility of a view depending on a state in vuex?

If I have a button <Button> Log me in! </Button>, and I want to dynamically change its style based on the state of my Vuex app (state.user is not null). How should I properly implement this functionality? I'm considering creating a field ...