Obtain the numerical representation of a weekday based on the name of

I am working with an array that looks like this:

result = ['Saturday','Sunday']

My goal is to return the index for each of the days above, like this:

detail= [6,7]

I attempted the following approach to achieve this, but unfortunately it did not work. I am new to working with Angular:

 var detail = result.getWeekday()

Answer №1

When working with string values like 'saturday' or 'sunday', it's important to remember that they are not inherently date values. To properly associate them with days of the week, you can either declare them as dates or assign a weekday index to them as shown below:

const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var selectedDays = ["Saturday","Sunday"];
var dayIndices = selectedDays.map(day => daysOfWeek.indexOf(day)+1);

By using Array.map, you can easily retrieve the weekday index for each element in your selectedDays array.

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

Factory function with type constraints and default parameter causing TS2322 error

I have a base class that requires some parameters to be passed... class BaseClass<ItemType> { // Some irrelevant parameters omitted for simplicity... constructor(__items: Iterable<ItemType>) {} } Now, I want to create a factory func ...

Modifying the date formatting in JavaScript if it is incorrect

Recently, I developed a mobile application that can scan QR-CODEs containing various information, including a date. However, there have been changes in the format of the date within the QR-CODE between previous and new versions. Previously, the date was fo ...

The parameter type 'Event' cannot be assigned to the argument type

edit-category-component.html: <custom-form-category *ngIf="model" [model]="model" (onSaveChanges)="handleChanges($event)"></custom-form-category> <mat-loader *ngIf="!model"></mat-loader> edi ...

Infer the types and flatten arrays within arrays

I am currently working on creating a custom function in typescript that can flatten nested arrays efficiently. My current implementation is as follows: function flattenArrayByKey<T, TProp extends keyof T>(array: T[], prop: TProp): T[TProp] { re ...

Having trouble utilizing HTML Canvas in the newest release of Angular and TypeScript

After extensive searching on the internet, I have not been able to find any working examples of using HTML canvas in Angular. It seems that changes in syntax in Typescript and Angular's newer versions have rendered all existing solutions obsolete. I ...

The custom directive validator is taking too much time to call the REST API, causing a delay in setting form.$invalid and

My input text field has a custom directive that triggers a REST Web Service Api call using $http (Restful Resource) and usually takes around 500ms. However, there is a chance that the user might submit the form before the call returns. How can I prevent th ...

Animating Angular for particular conditions

My goal is to create unique animations for specific state changes in my AngularJS app. I found inspiration from this tutorial: https://scotch.io/tutorials/animating-angularjs-apps-ngview, which works well. However, I am aiming for the following animations: ...

NestJS Bull queues - Failing to secure job completion with a lock

I am currently utilizing Bull in combination with NestJS to manage a jobs queue. Within the process handler, I aim to designate a job as failed instead of completed. However, it appears - after carefully reviewing the documentation as well - that the Job#m ...

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

Error message in Visual Studio 2017: Identical name 'URLs' declared twice in

In our Visual Studio 2017 project, we have multiple TypeScript files that define a URLs class. Each file contains different implementations of the class to change site URLs based on the specific use case: customer/urls.ts namespace Portal { export cl ...

Troubleshooting: Issue with Chrome's CSV Export Functionality in JavaScript/AngularJS

Currently, I am troubleshooting an issue with exporting a CSV file from a data table on a web application. The export functionality works fine on all browsers except for Chrome when the export button is clicked. I have been trying to solve this problem for ...

Strategies for transferring data between multiple controllers

Hello, I am just starting to learn AngularJS and have a basic understanding of controllers, which help transfer data to the view. Here is a sample controller based on what I know: angular.module('marksTotal',[]).controller('totalMarks&apos ...

Resolver problem involving Angular HttpClient

Encountering an issue when using Angular 2 with AOT compilation enabled (Angular universal) in a custom resolver. The error message received is as follows: Uncaught (in promise): Error Error: Uncaught (in promise): Error This problem appears to be oc ...

Achieving proper functionality of additional directives within uib-tab components

How can I utilize a callback function for uib-tab directives to refresh inner directives after tab rendering? I'm troubleshooting an issue with a third-party directive when used within the uib-tab directive from angular-bootstrap. The specific direct ...

Angular service sending an HTML file instead of a JSON file

After diving into MEAN stack technologies for the past week, I've encountered an issue while trying to utilize a custom service in Angular. My goal is to fetch a .json file, however, upon inspecting the loaded resources in the web inspector when the a ...

Eliminate attributes from an object that are not defined within a specific type interface

I am trying to filter out properties from an object that are not defined in a specific type interface. Suppose I have the following interface: export interface CreateCustomerUserInput { fullname: string; email: string; } And this is my initial o ...

Ways to display a vertical scrollbar within a select box

I'm currently working with an Angular select box and I'm looking to display a scroll bar if there are more than 5 data entries. <select class="form-control" data-ng-model='projectListData.appVersion' ng-selected ng-options="v.versio ...

Angular JS - Selecting Directives on the Fly

I'm currently developing an application where users can choose from various widgets using a graphical user interface (GUI). My plan is to integrate these widgets as angular directives. THE CONTROLLER $scope.widgets = ['one', 'two' ...

The subscription for the second Observable in RxJS concatMap is triggered individually

I am currently developing an Angular 6 application. I want the app to display a loading animation whenever there is a change in the route or if there are any pending HTTP requests. To achieve this, I have set up two Observables as follows: For httpPendingR ...

Troubleshooting Guide: Resolving the "No parameterless constructor defined for this object" error in your AngularJS/MVC application

I have a complex CRM page where users can schedule classes that they attend and learn from. The process involves filling out fields in a Modal and clicking Save, which then gathers the data and passes it to C#. However, in this particular instance, I&apos ...