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

Dealing with the challenges posed by middleware such as cookie access and memory leaks

Utilizing express with cookieParser() where my client possesses the cookie named: App.Debug.SourceMaps. I've crafted a middleware as follows: app.get('/embed/*/scripts/bundle-*.js', function(req, res, next) { if (req.cookies['App ...

Generating dynamic divs in parallel

Despite encountering numerous posts related to the issue at hand, none of them have solved my specific problem. I am aiming for 3 divs to display in each row, but the current code is causing each div to show up on a new line vertically: JQuery ...

Identify the credit card as either American Express or American Express Corporate

My JavaScript code can successfully detect credit card types, but I have encountered an issue. I am unable to differentiate between American Express and American Express Corporate cards. Is there a way to distinguish them or is it impossible to identify wh ...

Encountering an issue where props are receiving a value of undefined within a component that is

After receiving a JSON response from getStaticProps, I double-checked the data by logging it in getStaticProps. The fetch functionality is working smoothly as I am successfully retrieving the expected response from the API. import Layout from '../comp ...

Optimizing normals for unindexed BufferGeometry in Three.js

Currently, I am attempting to refine the normals of a mesh starting from an non indexed BufferGeometry. A similar query has been addressed in the past, however, the Three.js API has undergone significant changes since then and I am unable to make it work o ...

Creating an if statement that validates whether all variables have non-null values

I am still getting the hang of javascript and working on some coding projects from my textbooks. The current task involves creating an if statement to check if the values of the elements referenced by the names fname, lname, and zip are all not null. Here ...

Tips for effectively showcasing the counter outcome amidst the increase and decrease buttons

Currently, I am in the process of learning Angular and have created a component called quantity-component. Within the quantity-component.component.html file, I have implemented 2 buttons for increment (denoted by +) and decrement (denoted by -). The decrem ...

Is it possible to delay the loading of a page using location.href?

Trying to determine when a page has finished loading using location.href. Currently, my code is set up like this and I want certain events to occur once the page is fully loaded. I attempted using $.get but encountered issues with my existing implementatio ...

Using JSON data with an Android array

I am facing an issue with the code I have written to parse information received after sending a post request. The response looks like this: [{"fromUser":"Andrew"},{"fromUser":"Jimmy"}] My goal is to extract these users and display them in a list view. Bel ...

Dealing with 404 errors: The role of Nuxt.js and the srcDir option in handling URL

By incorporating the srcDir option into my nuxt.config.js file, I made the decision to transfer the pages, components, and layouts folders to the src directory. Below is how my configuration looks: module.exports = { srcDir: 'src', head: { ...

"Redirecting visitors based on their location using GeoIP targeting for

Is there a way to implement a code that redirects users based on their location? For example, if a user accesses the site from the United Kingdom, they should be redirected to /UK/, if from the US to /US/, and if from anywhere in the EU (excluding the UK) ...

Utilizing Laravel 5.3 and Vue.js for Dynamic AJAX Calls Based on Select Box Selections

I am looking to display a newly added record in a select box once it has been inserted into the table. Below is my Laravel HTML code: <div class="form gorup"> <select class="form-control" > <option @click="called" ...

React hook form submit not being triggered

import React, { useState } from "react"; import FileBase64 from "react-file-base64"; import { useDispatch } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import { TextField, Select, Input ...

Tips for showcasing the chosen option from an autocomplete input field in a React application

Currently learning React and working on a search feature for a multi-form application. The goal is to allow users to search for a student by first name, last name, or student ID using an autocomplete text field. The options for the autocomplete text field ...

Utilize VueJS to upload and visualize a file input on your website

I am currently working with TypeScript and Haml in conjunction with vue.js. My goal is to enable users to upload and view a file seamlessly using the vue.js framework. I have successfully managed to upload an image, however, I am facing an issue where the ...

Tips for optimizing the page speed of your Pixi JS app by ensuring it runs efficiently after the page loads

On my website, I implemented a dynamic gradient animation with shape-changing blobs using pixi js. The animation functions flawlessly, but the issue arises when I run page speed tests - the test assumes that the page has not finished rendering because the ...

Retrieve the specific array element from parsing JSON that includes a particular phrase

I need help filtering array values that contain the phrase 'Corp' Currently, all values are being returned, but I only want the ones with "Corp" var url = "/iaas/api/image-profiles"; System.debug("getImageProfiles url: "+url ...

Are you planning to print the JSON information?

I've been working on mastering JavaScript and decided to create a script that displays all public Facebook statuses related to a specific keyword. However, things aren't going as planned. You can find a sample URL where the JSON data is located h ...

Converting a unix timestamp to a Date in TypeScript - a comprehensive guide

To retrieve the unix timestamp of a Date in plain JavaScript and TypeScript, we can use this code snippet: let currentDate = new Date(); const unixTime = currentDate.valueOf(); Converting the unix timestamp back to a Date object in JavaScript is straight ...

Automate the process of modifying specific data in tables through Javascript

I've been attempting to replicate the appearance of a stock-exchange board, but I'm encountering difficulties in updating text automatically without interrupting another. My attempts so far: var textPositive = ["2.0%", "1.7%" ...