how can I retrieve an element using a datetime in JavaScript

I have a list of appointments like the one below:

0: {start: '2022-02-23T09:30:00', end: '2022-02-23T10:00:00',…}
    1: {start: '2022-02-23T10:00:00', end: '2022-02-23T10:30:00',…}
    2: {start: '2022-02-23T10:30:00', end: '2022-02-23T11:00:00',…}
    ...

My goal is to find the first appointment that has free time in between its start and end

For instance, if we examine the third appointment, it starts at 11:00 and ends at 11:30 with no gaps before. The next appointment starting at 12:00 creates a 30-minute gap from 11:30 to 12:00. I am looking to retrieve the end property (or the entire appointment) of the first appointment without a direct follow-up.

Update: I have attempted the following code snippet

const latestAppointment = [...appointments].sort((a, b) => (new Date(a.end)) < (new Date(b.end)) ? -1 : 1)

I hope this explanation clarifies my question. Thank you.

Answer №1

Are you in search of a solution like this?

myArray.filter((e, i, o) => e.end !== o[i+1]?.start)?.shift();

const schedule = {
  0: {
    start: '2022-02-23T09:30:00',
    end: '2022-02-23T10:00:00'
  },
  1: {
    start: '2022-02-23T10:00:00',
    end: '2022-02-23T10:30:00'
  },
  2: {
    start: '2022-02-23T10:30:00',
    end: '2022-02-23T11:00:00'
  },
  3: {
    start: '2022-02-23T11:00:00',
    end: '2022-02-23T11:30:00'
  },
  4: {
    start: '2022-02-23T12:00:00',
    end: '2022-02-23T12:30:00'
  },
  5: {
    start: '2022-02-23T13:00:00',
    end: '2022-02-23T13:30:00'
  },
  6: {
    start: '2022-02-23T13:30:00',
    end: '2022-02-23T14:00:00'
  },
  7: {
    start: '2022-02-23T14:00:00',
    end: '2022-02-23T14:30:00'
  },
  8: {
    start: '2022-02-23T14:30:00',
    end: '2022-02-23T15:00:00'
  },
  9: {
    start: '2022-02-23T15:00:00',
    end: '2022-02-23T15:30:00'
  },
  10: {
    start: '2022-02-23T15:30:00',
    end: '2022-02-23T16:00:00'
  },
  11: {
    start: '2022-02-23T16:00:00',
    end: '2022-02-23T16:30:00'
  },
  12: {
    start: '2022-02-23T16:30:00',
    end: '2022-02-23T17:00:00'
  }
}


const result = Object.values(schedule).filter((e, i, o) => e.end !== o[i+1]?.start)?.shift();
console.log(result);

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

Upgrading an Express 2.x application to Express 3.0

I am currently studying NodeJs and Express and am in the process of converting a tutorial app from Express 2.5.9 to version 3.0. The following code is now causing an error "500 Error: Failed to lookup view "views/login". How can I update this to render cor ...

Using AJAX to showcase data from a table in a database using the <select> tag but with an unfinished submit process

I am encountering an issue with my code. When I submit the value in getinv.php, it only returns a partial value. For example, when I click '2016-08-27', it only retrieves '2016'... My question is, how can I ensure that the exact value ...

Organize an AngularJS dataset into pages

I've organized some JSON (dummy) data into a users table. let website = 'https://jsonplaceholder.typicode.com'; // Setting up an Angular module called "usersApp" let app = angular.module("usersApp", []); // Creating a controller for the ...

A helpful guide on performing a service call in AngularJs when attempting to close the browser tab or window

I am currently working on implementing a service call that will trigger when the browser tab or window is being closed. I was wondering if there is a way to make a RestApi call when attempting to close the browser tab or window. Does anyone have any sugge ...

Is it possible for a popup to appear without any user interaction

Do you ever wonder how certain websites are able to trigger pop-ups without being blocked by Chrome's pop-up blocker? I had always thought that pop-up blockers only allowed window.open if it was initiated by a user action. However, the situation seem ...

How can I programmatically trigger a change event for a dropdown in Vue.js?

I am looking to trigger a dropdown change event within a Vue.js method. <select id="idDropdown" v-model="Result.Value" v-on:change.prevent="changeSelection($event, 'somevalue')"> How can I call the above `cha ...

Retrieving the original state value after updating it with data from local storage

Incorporating the react-timer-hook package into my next.js project has allowed me to showcase a timer, as illustrated in the screenshot below: https://i.stack.imgur.com/ghkEZ.png The challenge now lies in persisting the elapsed time of this timer in loca ...

Callback for Vue asynchronous components

<template> <div> <DynamicComponentA></DynamicComponentA> <!-- Display DynamicComponentB only after DynamicComponentA is loaded --> <DynamicComponentB></DynamicComponentB> </div> ...

What steps do I need to take in order to develop a custom component for FormControls?

Trying to create a form with a custom component for controls, I encountered an issue. Despite including the new component in the parent form that already has a formGroup, Angular throws an error. The error I faced is: Error: formControlName must be use ...

Initializing variables in Angular2 templates

I am facing an issue where, upon running the application, the console displays all the logs from the ngOnInit function, but the actual view only shows a skeleton without the component variables and text from l18n. It seems like the ngOnInit is not working ...

When a function is included in an object, it transforms into something other than a function

In my model, it looks like this: export default class UserObject { name: string; id: string; validateInsert() { } } If I interact with the model in this way: const modelUser: UserModel = new UserModel(); modelUser.ID = 1; modelUser ...

Tips on incorporating asynchronous functionality in AngularJS

I am currently utilizing AngularJS version 1.5.8 and have a specific requirement. When the user clicks on the Next button, the text inside the button should change to 'Processing...' before completing the operation. I have implemented the $q serv ...

What is the process for transforming a string literal type into the keys of a different type?

Imagine having a string literal type like this: type Letters = "a" | "b" | "c" | "d" | "e"; Is there a way to create the following type based on Letters? type LetterFlags = {a: boolean, b: boolean, c: bool ...

How to Implement Modal Popups on Click in Angular with the AmCharts XY Chart

Our goal is to display a modal window when users click on a data point. The current code we are using is as follows: constructor(public dataservice: DataserviceService, private modalService: NgbModal, private router: Router) { } ... ... bullet.events.on( ...

Cover the whole page in darkness and emphasize the division element

I attempted to implement a solution I found on stackoverflow without success. The issue is that the blackout feature does not seem to activate - nothing happens. Within my JavaScript code (all enclosed in the $(document).ready(function () tag), it appears ...

"Exciting developments in Angular 17 with the introduction of the new @

I need to output elements from an array of strings starting at index 1. arr = [ "str1", "str2", "str3", "str4", "str5" ] The desired output is: str2 str3 str4 str5 To achieve this, use a new @for loop in ...

During the signin process with invalid credentials, a CallbackRouteError is thrown by Next-auth instead of the expected CredentialsSignin error while using the Credentials

I am currently engaging with the tutorials provided by Next JS, accessible at Next JS. Right now I am immersed in chapter 15. However, I encountered a peculiar issue when attempting to sign in with invalid credentials. Instead of receiving the expected err ...

Turn off the validation of individual JavaScript errors in Eclipse

Currently, I am exploring the use of Eclipse for JavaScript within the "Eclipse IDE for Java EE Developers" package. In my project, there is a heavy reliance on Bluebird, a promises implementation, resulting in several lines like: somePromise.catch(funct ...

Unable to add items to the collection in NPM with Meteor 1.3

I have encountered an issue with the imap-simple NPM package while trying to perform an insert operation. Despite following the suggestions on , I am still unable to get the insert function to work properly! Even after simplifying the code and eliminatin ...

Nested state fails to display HTML content or activate controller initialization

I am trying to utilize ui-router's parent/child states with the code snippet below. Everything seems to be in order as there are no console errors, but for some reason the HTML content is not being rendered and the controller's init function is n ...