Angular 6: Extracting an array from another

I am dealing with an array of IVisitView objects containing visitDate and clientName information.

export interface IVisitView {
  visitDate?: Moment;
  clientName?: string;
}

export class VisitView implements IVisitView {
  constructor(
    public visitDate?: Moment,
    public clientName?: string,
  ) {}
}

Is there a way to transform the array so that the visitDate property is converted to a String type instead of Moment?

Answer №1

Employ the .map() method with a callback function that takes x as an argument and formats its visitDate property to display properly.

Answer №2

interface IVisitView {
  visitDate?: Date | string;
  clientName?: string;
}

class VisitView implements IVisitView {
  constructor(
      public visitDate?: Date | string,
      public clientName?: string
  ) {}
}

const createVisit = (date: Date, name: string) => new VisitView(date, name);

const visit1 = createVisit(new Date(), 'UserOne');
const visit2 = createVisit(new Date(), 'UserTwo');
const visit3 = createVisit(new Date(), 'UserThree');

const visitsArr: Array<VisitView> = [visit1, visit2, visit3]

const updatedVisits: Array<IVisitView> = visitsArr.map((v: IVisitView) => {
    if (v.visitDate instanceof Date) {
        v.visitDate = v.visitDate.toDateString();
    }
    return v;
});

console.log(updatedVisits);

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

Steps to incorporate padding to a nested Angular Component by leveraging the CSS of its parent component

It is evident from the example at https://material.angular.io/components/expansion/examples that material components can be customized using the CSS of the component embedding them: .example-headers-align .mat-form-field + .mat-form-field { margin-left: ...

Encountering difficulties with installing bootstrap-vue

While attempting to integrate Bootstrap-Vue into my project that includes Vuex, Vue-Router, TypeScript, and Babel, I encounter an error in the browser. To replicate docker run -it --rm -p 8080:8080 node:17.7.2-alpine yarn global add @vue/cli vue create ...

The problem encountered with Angular ngrx: TypeError when attempting to freeze array buffer views containing elements

I'm running into a problem with ngrx. I have an array in my state to which I am trying to add objects. Everything seems to be working fine as I can see the values in my store when I console log them. However, the redux dev tools and console are throwi ...

How can esbuild be used to load .wglsl files in Typescript files?

I'm currently utilizing esbuild to bundle my Typescript code, but I'm facing a challenge with configuring a loader for ".wgsl" files. Here is my app.ts file: import shader from './shader.wgsl'; //webgpu logic This is my shader.d.ts fi ...

What is the reason behind Rxjs switchMap only emitting the final value from an of() observable source?

Here are two code snippets, one using map and the other using switchMap. The functionality of map is clear: of('foo', 'bar') .pipe(map((val) => sanitizer(val))) .subscribe((val) => console.log('value:', val)); func ...

Compilation of Zod record with predefined keys

I need to create a dictionary similar to a zod schema for an object with multiple keys that are already defined elsewhere, all sharing the same value type. Additionally, all keys must be required. const KeysSchema = z.enum(['a', 'b', &a ...

What is the best way to send out Redux actions?

I'm in the process of creating a demo app with authorization, utilizing redux and typescript. Although the action "loginUser" in actions.tsx is functioning, the reducer is not executing as expected. Feel free to take a look at my code below: https:/ ...

Angular2: Retrieve and process a JSON array from an API

I'm currently facing an issue with my service while attempting to fetch a json array from an api and showcase it on the page. I believe there might be an error in my code, but I can't pinpoint exactly where. getAuctions(): Promise<Auction[ ...

What steps can I take to resolve a CSS problem in an Angular Web Component within a React Application?

I recently integrated an Angular Web Component with some widgets from Angular Material UI into my simple React Application. While the functionality of the buttons, tables, and radio buttons is working perfectly fine, I am facing issues with the styling and ...

The property 'dateClick' is not found in the 'CalendarOptions' type in version 6 of fullcalendar

Below is the Angular code snippet I am currently using: calendarOptions: CalendarOptions = { plugins: [ dayGridPlugin, timeGridPlugin, listPlugin ], initialView: 'dayGridMonth', headerToolbar: { left: 'prev today next' ...

Can a TypeScript variable in Angular contain a mixture of HTML and plain text?

I have a website where I am displaying content from a Model file. I would like to create a TypeScript variable that contains both a string related to the website's content and a URL enclosed in an HTML tag. When this variable is rendered on the view, ...

Consolidation of files for Client-Code-Generation with Swagger-Codegen: What is the best way to merge all files into

Just recently started using Swagger and NodeJS, I successfully integrated Swagger into my NodeExpress application and generated typescript-client-code with Swagger-Codegen (specifically Typescript-Angular). However, the issue I am facing is that the gener ...

Typescript encounters issues when assigning declaration as TRUE

Currently, I'm working on a project in Angular 2 and attempting to create TypeScript definitions for it so that it can be exported as a library. I have various services set up that make HTTP requests to components, all structured similarly to the cod ...

Utilizing Typescript Decorators to dynamically assign instance fields within a class for internal use

I am interested in delving into Typescript Decorators to enhance my coding skills. My primary objective is to emulate the functionality of @Slf4J from Project Lombok in Java using Typescript. The concept involves annotating/decorating a class with somethin ...

What is the maximum allowable size for scripts with the type text/json?

I have been attempting to load a JSON string within a script tag with the type text/json, which will be extracted in JavaScript using the script tag Id and converted into a JavaScript Object. In certain scenarios, when dealing with very large data sizes, ...

Navigating through pages: How can I retrieve the current page value for implementing next and previous functions in Angular 7?

Greetings, I am a new learner of Angular and currently working on custom pagination. However, I am facing difficulty in finding the current page for implementing the next and previous functions. Can anyone guide me on how to obtain the current page value? ...

How can I make TypeScript properly export function names for closure-compiler?

Here is the TypeScript code I am working with: namespace CompanyName.HtmlTools.Cookie { export function eraseCookie(name:string, path:string) { createCookie(name, "", path, -1); } export function readCookie(name:string) { ...

What is the best approach to creating customizable modules in Angular2?

I'm exploring the most effective approach to configuring modules in Angular 2. In Angular 1, this was typically achieved through providers. As providers have been altered significantly, what is the preferred method for passing configuration parameters ...

When trying to save a child entity, TypeORM's lazy loading feature fails to update

I've been troubleshooting an issue and trying various methods to resolve it, but I haven't had any success. Hopefully, someone here can assist me. Essentially, I have a one-to-one relationship that needs to be lazy-loaded. The relationship tree ...

Passing data between two distinct components when a button is clicked within Angular 4, all while ensuring the route URL remains unchanged

Having recently started working with Angular 4, I encountered an issue when trying to pass data from one component to another without using ActivatedRoute to send the data via the URL. In my searchOption.component.html file, the Submit button code looks l ...