Guide on navigating an array of objects using the provided keys as a starting point in Javascript/Typescript

Assuming I have an array of objects structured like this:

const events: Array<{year: number, month: number, date: number}> = [
 {year: 2020, month: 10, date: 13},
 {year: 2021: month: 3, date: 12},
 {year: 2021: month: 9, date: 6},
 {year: 2021: month 11, date 23},
]

If I specify a starting point of January 10th, 2021 within the array, how can I create a function that will return all dates from that starting point onward?

Answer №1

If you find yourself needing specific items, consider utilizing the filter method of Array.prototype to isolate them.

When creating a comparison function, I recommend converting each item into a Date Object. This allows for easier comparison by milliseconds, helping identify items that fall after your desired date.

Answer №2

Along with Fabio's response, here is a way to achieve the desired outcome:

function getFilteredDates(datesList: any, startDate: Date) {
    return datesList.filter(item => item.year >= startDate.getYear() && item.month >= startDate.getMonth() && item.date >= startDate.getDate())
}

getFilteredDates(arrayOfDates, new Date(2021, 0, 10)) // yields three arrays

This code assumes that months in JavaScript Date class are indexed starting from 0.

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

Creating a dynamic columns property for Mat-Grid-List

Is it possible to create a Mat-Grid-List where the number of columns can be dynamically changed based on the width of the container? Here is an example: <mat-grid-list [cols]="getAttachmentColumns()" rowHeight="100px" style="width: 100%;"> <mat ...

Retrieve progress with easing using jQuery's animate() function

At the moment, I'm utilizing this code to create an animation for a bar-graph-inspired element: $('.anim').animate({ right: `${100-(e/max*100)}%`, backgroundColor: colors[0] },{ duration: 1500, easing: 'easeInQuart&apos ...

Incorporating a JavaScript workflow into Django

Currently, I am following a tutorial on integrating a modern JavaScript pipeline into my Django application. The objective is to have the JavaScript code write "Hello webpack" onto the page, but unfortunately, it is not displaying as expected. Since I alr ...

The Vue component should trigger the display of data in a Bootstrap modal based on the row of the button that was

Here is a sample code snippet demonstrating how data is fetched from the database: <table class="table table-bordered"> <thead> <tr><th>User ID</th><th>Account Number</th><th>Accou ...

execute function once eventlistener completes running

I've implemented a code snippet to detect the availability of a gyroscope for user interaction. Here's how it works: function check_user_hardware(){ window.addEventListener("devicemotion", function(event){ if(event.rotationRate.alpha ...

Execute function prior to redirection of iframe

I have a specific challenge with an iframe on my webpage. I am looking to trigger a function right before the iframe reloads a new page, rather than after it has finished loading. I need this function to be triggered before the iframe redirects to the new ...

Executing a message in Azure Service Bus using Javascript/Node.js

I am utilizing the @azure/service-bus library in a Node.js application to receive messages. The code I am using is as follows: const { ServiceBusClient } = require("@azure/service-bus"); const sbClient = new ServiceBusClient(connectionString); ...

Avoiding redundancy by establishing the loading state in a redux reducer

Let's dive into a concrete example to better illustrate my point. In the webapp I'm working on, users can apply for jobs using a job reducer that handles various actions such as creating_job, created_job, fetching_job, fetched_job, fecthing_jobs, ...

"What is the best way to transform a POST curl request into an Angular HTTP POST request using gsutil for Google Cloud Storage

Currently utilizing GCP and GCS. I am attempting to send a POST request, however in the curl command below, I am unable to locate where to input the necessary data that needs to be sent. In this scenario, how can I implement a POST request using angular ...

Convert my information to an XML document

Successfully, I have loaded the content of an XML file into my PHP document using the following method: $(document).ready(function () { $.ajax({ type: "GET", url: "abstimmer.xml", dataType: "xml", success: function ...

What is the best way to transfer a table row from one table to another in AngularJS?

I need assistance with a feature that allows users to move rows between tables by clicking on buttons. When the 'up' button is clicked, the 2nd table row should be moved to the 1st table, and when the 'down' button is clicked, the 1st t ...

Tips for sending and retrieving parameters using the POST technique

Currently, I am in the process of building a user authentication form for my website using Javascript. I am utilizing Vue JS on the client-side and NodeJS with ExpressJS on the server-side. For the server-side functionality, I have implemented the followi ...

How can I preserve the line break in a textarea using PHP?

Is it possible to maintain line breaks in a textarea using PHP? Currently, I have a temporary solution that involves using the exec function to run a shell command, but I would prefer a purely PHP approach. Below is my temporary script - can you help me mo ...

The symbol 'router-outlet' is not recognized by the system

This new project utilizes Angular 13 for its implementation. One of the key updates is the addition of routing to the project, specifically in the file app-routing.module.ts. import { NgModule } from '@angular/core'; import {RouterModule, Routes ...

What are the necessary steps to deploy Angular 2 universal starter on a third-party server host such as Google Cloud or Azure?

I recently cloned the universal-starter (webpack version) repository and successfully got it running on my local machine using npm start and npm run watch as per the provided instructions. However, I hit a roadblock after running npm run build and trying ...

Saving documents in a PostgreSQL database with the help of NodeJS and Angular2+

I have researched various tutorials and examples on the internet for uploading and storing files in PostgreSQL using NodeJS, Multer, Sequelize, Express, but haven't found a solution that works for my project. My project consists of an Angular app wit ...

Avoiding the utilization of automatically generated JSON files as a data source in Rails when

I have implemented javascript code that uses JSON to generate a timeline. The JSON is being created using json_builder and it contains only the current user's information. The JSON displays correctly when accessed directly through its URL, but when th ...

Guide for verifying the minimum and maximum values when selecting a particular product from a dropdown menu

My goal is to implement validation for the width textbox when a user selects a specific product from the dropdown menu. The validation should only allow the user to enter a value between 10 and 200. I have successfully retrieved the selected value, but I ...

What can you do with jQuery and an array or JSON data

Imagine having the following array: [{k:2, v:"Stack"}, {k:5, v:"Over"}, , {k:9, v:"flow"}] Is there a way to select elements based on certain criteria without using a traditional for/foreach loop? For example, can I select all keys with values less than ...

What is the process of adding a unique model to three.js?

Despite trying numerous solutions to a common problem, I am still unable to import my 3D model in Three.js using the following code: <script src="https://threejs.org/build/three.min.js"></script> <script src="https://cdn.rawgi ...