What is the best way to change a Date stored in an array to a string format? [angular4]

Presented here is an array with the data labeled dateInterview:Date:

  public notes: Array<{ idAgreement: string, note: string, dateInterview: Date }> = [];

My goal is to send this array to the server where all values of dateInterview need to be converted to strings. How can I achieve this?

While I was able to convert the value using onChange in my input, it's not a feasible solution for various reasons.

This snippet demonstrates how I handle the conversion in my code:

this.defaultAgreement.interviews = this.notes;

I am assigning notes to defaultAgreement.interviews which is nested within an Object:

...,
public interviews: Array<any>,
....,

Subsequently, I send this comprehensive object that includes interviews.

Your assistance with this matter is greatly appreciated!

Answer №1

One method to transform elements in an array is by using the Array.prototype.map

let updatedArray = notes.map(({ idAgreement, note, dateInterview }) => ({ idAgreement, note, dateInterview: dateInterview.toString() }));

Answer №2

After reviewing my comment, the most efficient solution would be:

const notesWithStringDates = this.notes
  .map(note => ({ ...note, dateInterview: note.dateInterview.toISOString() }))

const notes = [
  { date: new Date(), id: 0 }
];

const mapped = notes
  .map(note => ({ ...note, date: note.date.toISOString() }))
  
console.log(mapped);

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

Retrieving key values from an interface using Typescript

export interface Cookies { Token: string; SessionID: string; UserID: string; } type property = keyof Cookies // property is "Token" | "SessionID" | "UserID" export const COOKIE_PROPERTIES: Record<property, property& ...

Extending the type of parameters in TypeScript

I am trying to call a function from my UI library with a parameter type that extends the original (Suggestion) type by adding more properties. I found a resource that suggests it is possible here: https://github.com/Microsoft/TypeScript/issues/2225 (in the ...

The 'disabled' property is not found in the 'MatButton' type, however, it is necessary in the 'CanDisable' type

Issue found in node_modules/@angular/material/core/option/optgroup.d.ts: Line 17: Class '_MatOptgroupBase' does not correctly implement interface 'CanDisable'. The property 'disabled' is missing in type '_MatOptgroupBas ...

The conversion of an array to Ljava/lang/Object is not possible

I'm currently working on a project using NativeScript app with TypeScript where I am trying to pass an array of android.net.Uri to a function. However, when attempting to do so, I encounter an error mentioning that the 'create' property does ...

Encountering error TS2305 in Visual Studio Code while working with moment.js in an example from an Angular Material

After referencing the code snippet from https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings, I encountered an issue. While the code successfully compiles and runs, Visual Studio Code flagged an ...

The incorrect argument is being used to infer the generic type

I have implemented the NoInfer feature from the library called ts-toolbelt. However, it seems that the example provided below does not reflect its intended effect. In this scenario, the generic type is deduced from the util function as soon as it is intr ...

The extraction process for @agm/[email protected] failed the verification check

Attempting to add npm to my project by running npm install results in the following error message: npm ERR! code EINTEGRITY npm ERR! Verification failed while extracting @agm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e ...

Enhancements to a NativeScript Application

After running some tests on my NativeScript app following the steps outlined here - , I found that it takes 18 seconds for the program to start and for a user to log in. Is this considered acceptable performance? Appreciate any feedback provided! ...

Converting data received from the server into a typescript type within an Angular Service

Received an array of Event type from the server. public int Id { get; set; } public string Name { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } For Angular and TypeScript, I need to transform it into the following clas ...

Can you explain the distinction between "parser" and "parserOptions.parser" in an ESLint configuration?

For a considerable amount of time, I have been using the TypeScript and Vue presets provided below. While it has been functional, I realize that I do not fully comprehend each option and therefore seek to gain a better understanding. Firstly, what sets apa ...

Guide to correcting the file path of an external css within the public directory on Express framework

I am facing an issue with loading external CSS files and need some help to fix the path. Despite multiple attempts, I have been unsuccessful so far. Below is my category structure: https://i.stack.imgur.com/sLTcN.png Within the header.ejs file, this is h ...

Understanding the mechanisms of Promise functionality within Typescript can be challenging, particularly when encountering error messages such as "type void is not

Recently, I've delved into the world of Typescript. Despite my efforts to stay true to the typing system, I've encountered a challenge that forces me to resort to using the any type: The issue arises with a function that returns a promise: sav ...

Combining all code in Electron using Typescript

I am currently working on developing a web application using Electron written in Typescript and I am facing some challenges during the building process. Specifically, I am unsure of how to properly combine the commands tsc (used to convert my .ts file to ...

Error: Attempting to access the 'user' property of a null value

I need help passing a unit test for my isAdmin() function. The function is supposed to return a role, but I keep getting an error that says 'user' cannot be read. I think the issue lies in how I am handling the user information provided by curren ...

Having difficulty connecting to the router within a container component for Angular2 routing

Presently, my code snippet looks like this: import { Router } from 'angular2/router'; @Component({...}) export class Index { constructor(public router: Router) { this.router.subscribe({...}); } } Although there are additional function ...

Automating email testing for Azure Graph-built applications: A step-by-step guide

I am exploring options to streamline the email testing process for an application developed with Azure Graph integration. Currently, I am leveraging playwright and typescript for other testing purposes within the application. One of the key functionaliti ...

Testing the Angular component with service: An error occurred while trying to access the `diagnosticData` property as it was found to be undefined

Seeking guidance on angular testing as a beginner. I have encountered an issue where my component is not receiving the values during testing, even though the app functions correctly. Can someone assist me with this? I have shared the service, JSON object, ...

What is the proper way to effectively utilize modules in Angular?

As a beginner in Angular, I've been debating whether it's better to have one module for each component or to work without custom modules. In the past, I typically created a component and passed it to declarations in app.module.ts. However, I rece ...

bespoke session with Next.js using Next-Auth

I encountered an issue while migrating my JS file to TSX. What I am trying to do is sign in with credentials and customize the session user to my user data. // api/auth/[...nextauth].js import NextAuth from "next-auth"; import Providers from &qu ...

The Validator.js module cannot be located - Unable to resolve 'http' in the client

Struggling with integrating the amadeus-node package into an angular 10 project. When I add the following line: const Amadeus = require('amadeus'); I encounter this error: ERROR in ./node_modules/amadeus/lib/amadeus/client/validator.js Module ...