Is it possible for me to modify the Date type properties within the get method?

I have a function that retrieves a list of items

getDate() {
this.http.get(this.url, this.httpOptions)
.subscribe((res: any ) => {
  this.list = res.list;
  this.list.forEach(element => {
    return this.datePipe.transform(element.startTime, 'yyyy-MM-dd');
  });  
});
} 

I want to format the date before displaying it. Is it possible to use DatePipe within the getDate function for this purpose?

Answer №1

Give this a shot:

retrieveData() {
  this.http.get(this.apiUrl, this.httpOptions)
    .pipe(
      map(response => response.data.map(item => ({
        ...item, 
        startDate: this.dateFormatter.transform(item.startDate, 'dd-MM-yyyy')
      )})))
     ).subscribe((result: any ) => {
       this.dataList = result.data;
     });  
}

Answer №2

Utilize the Observable.pipe() method from Rxjs library to transform data.

For example:

 this.http.get(this.url, this.httpOptions).pipe(
     map(val => val = this.datePipe.transform(val.startTime, 'yyyy-MM-dd'))
  ).subscribe((res: any ) => {
    this.list = res.list;
  });

update: Since your object appears to be a list, consider using map() to modify each element individually.

Explore more at: https://angular.io/guide/rx-library#operators

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

I am facing numerous build errors while working with Angular's CDK library

Currently, I am working on drag and drop implementation in Angular and have installed angular cdk for that purpose. However, when I try to run npm start, I encounter an endless stream of errors, all stemming from the cdk node modules. Here is a glimpse of ...

Leverage lodash operators within nested object properties

My data consists of an array of objects { "agent_name": "AgentName", "analytics": [ { "date": "Tue, 1 Aug 2021 00:00:00 GMT", "intents_count":[ { "coun ...

Failure to validate the API in accordance with the database

Having an issue with login validation in Angular code while using Spring Boot backend. Even when providing incorrect credentials, the login form still shows as successful. Need help troubleshooting this problem. 1) auth.service.ts import { HttpClient ...

By utilizing the subject and observable as input/output service parameters, we can break free from reliance on the traditional HTTP request/response setup and the service's reliance on

I am currently developing a service that requires the opening of a modal and triggering a series of http calls based on specific user interactions. The APIs that need to be called vary depending on the context in which the service is used, as well as the A ...

Having trouble with installing npm package from gitlab registry

I recently uploaded my npm package to the GitLab package registry. While the upload seemed successful, I am facing an issue trying to install the package in another project. When I run npm install, I encounter the following error: PS E:\faq\medu ...

Generating RSA Public Key with JavaScript or TypeScript

I am facing an issue with my c# web api and React app integration. The problem arises when trying to reconstruct a public key in the frontend for encryption purposes. Despite generating public and private keys in c#, I am struggling to properly utilize th ...

Problem encountered when trying to deploy a Next.js application with React Hook Form v7 on Vercel platform

I am currently in the process of creating a web application using nextjs and chakra UI while incorporating typescript. I've integrated react hook form for form validation, however I encountered a problem when deploying it on vercel. Check out the scre ...

Can you provide instructions on executing package dependencies using yarn in the command line? For example, is there a command similar to npx tsc init for initializing a npm

When utilizing yarn, the node_modules folder is not present. Instead, dependencies are stored in a .yarn/cache folder. I attempted to use yarn dlx tsc init and npx tsc init, but they did not achieve the desired result. There are various development depend ...

Elegantly intersect two types of functions in Typescript

Two function types are defined as follows: wrapPageElement?( args: WrapPageElementBrowserArgs<DataType, PageContext, LocationState>, options: PluginOptions ): React.ReactElement .. and .. wrapPageElement?( args: WrapPageElementNodeArgs<Data ...

Building a frontend and backend using Typescript with a shared folder for seamless integration

I am currently exploring the idea of transitioning to TypeScript, but I am facing challenges in figuring out how to create a shared folder between the frontend and backend. This is the project structure that I have come up with: frontend - src -- server.t ...

The React Nested Loop Query: Maximizing Efficiency in Data

Learning React has been a challenge for me, especially when comparing it to XML/XPath. In this scenario, I have two arrays simplified with basic string properties... customerList: Customer[] export class Customer { id: string = ""; firstnam ...

Absolute file path reference in Node.js

I'm working on a Node.js project using WebStorm IDE. Here's the structure of my project: The root folder is named "root" and inside are 2 folders: "main" and "typings". The "main" folder has a file called "foo.ts", while the "typings" folder co ...

Utilize the click functionality in Angular along with Openlayers to identify the specific layer that has been clicked

Currently, I am working on an openlayers map project and need a way to identify the layer I am clicking on. This information is crucial as I plan to retrieve specific data from geoserver regarding the clicked layer. In the past, I managed to achieve this ...

Successive API calls - Observable operations

Is there a way to sequentially call APIs? For instance, before inserting data into the database, I need to inform the user if they have entered duplicate key information. The first step is to use a GET API to check if the record already exists in the data ...

Error: User authentication failed: username: `name` field is mandatory

While developing the backend of my app, I have integrated mongoose and Next.js. My current challenge is implementing a push function to add a new user to the database. As I am still relatively new to using mongoose, especially with typescript, I am followi ...

The specified dependency, * core-js/fn/symbol, could not be located

I am in the process of developing a Vue.js application with Vuex and have encountered some errors during the build. I attempted to resolve the issue by installing npm install --save core-js/fn/symbol, but unfortunately, it did not work as expected. https:/ ...

Angular validation to ensure future dates are not selected

I am interested in exploring Angular's datepicker functionality and implementing date validation to restrict the selection of future or past dates. For example, if today's date is 17/12/2020, users should only be able to select the current date ( ...

"Encountering the error message "Uncaught TypeError: $(...).summernote is not a function" while working with

I've encountered an issue while trying to implement summernote into my Angular app. I keep receiving the error message "$(...).summernote is not a function" and it seems like summernote is not loading properly on the page. I'm unsure of what step ...

Angular 2: Patience is a Virtue When Dealing with Observables

Dealing with multiple asynchronous calls can be tricky, especially when you need to wait for all of them to finish before moving on to the next step. In my case, I have separate calls that may or may not be made depending on user input. How can I efficient ...

Component-driven form validation in Angular 2

I'm facing a challenge with implementing model-driven form validation in my custom input component. Specifically, I need to figure out how to pass ngControl to the component. In the plunkr demo provided at http://plnkr.co/edit/QTmxl8ij5Z6E3xKh45hI?p= ...