A guide on transferring data between two arrays of objects using TypeScript

I am working on implementing a get request within a component that retrieves a response:

  
getPaymentIntents(): Observable<Payment>{
  const url: string = 'https://store.com//payments';

  return this.http.get<Payment>(url);
}

The response data structure for the "Payment" type is as follows:

[
  {
    "id": "pi_3K4B432423dqM1gTYncsL",
    "amount": 2000,
    "amount_capturable": 0,
    "amount_received": 0,
    "application": null,
    "canceled_at": null,
    "cancellation_reason": null,
    "created": 1638911287,
    "currency": "usd",
    "customer": "cus_KjDBkdsaHIT6AN"
  },
  {
    "id": "pi_3K4BW7EE9YQoA1qM1gTYncsL",
    "amount": 1000,
    "amount_capturable": 0,
    "amount_received": 0,
    "application": null,      
    "canceled_at": null,
    "cancellation_reason": null,
    "created": 1638913687,
    "currency": "usd",
    "customer": "cus_KjDBkxEVHIT6AN"
  }
]

I aim to display this data in a Material Table. I only need to showcase a subset of the information and plan to incorporate another response's data into the table in the future.

The desired interface for passing data to the table as a dataSource should match the following format:


export interface OrderToProcess{
  Id: string,
  Amount: number,
  Currency: string
}

I have been experimenting with methods like `filter()`, `map()`, `Object.entries()` to transform one type into another but haven't achieved the desired outcome yet. Any guidance or assistance would be greatly appreciated!

Answer №1

You are on the right track with your search for map. Let's assume we have a function set up to mimic an HTTP call:

  // Necessary imports for the example code
  import { map, Observable, of } from 'rxjs';

  /**
   * getPaymentIntents simulates an HTTP call using static data
   */
  getPaymentIntents(): Observable<any> {
    return of([
      {
        id: 'pi_3K4B432423dqM1gTYncsL',
        amount: 2000,
        amount_capturable: 0,
        amount_received: 0,
        application: null,
        canceled_at: null,
        cancellation_reason: null,
        created: 1638911287,
        currency: 'usd',
        customer: 'cus_KjDBkdsaHIT6AN',
      },
      {
        id: 'pi_3K4BW7EE9YQoA1qM1gTYncsL',
        amount: 1000,
        amount_capturable: 0,
        amount_received: 0,
        application: null,
        canceled_at: null,
        cancellation_reason: null,
        created: 1638913687,
        currency: 'usd',
        customer: 'cus_KjDBkxEVHIT6AN',
      },
    ]);
  }
}

In your component, you can map this data to your desired type as shown below (or with similar logic):

  // Define table columns
  displayedColumns: string[] = ['id', 'amount', 'currency'];
  // Data source for our table
  dataSource$: Observable<OrderToProcess[]>;
  constructor() {
    this.dataSource$ = this.getPaymentIntents().pipe(
      // Map data to an array of type OrderToProcess
      map((data) => {
        let transformedData: OrderToProcess[] = [];
        for (let i = 0; i < data.length; i++) {
          transformedData.push({
            Id: data[i].id,
            Amount: data[i].amount,
            Currency: data[i].currency,
          });
        }
        return transformedData;
      })
    );
  }

To display this in a table, you can use the following structure:

<table mat-table [dataSource]="dataSource$" class="mat-elevation-z8">
  <ng-container matColumnDef="id">
    <th mat-header-cell *matHeaderCellDef>ID</th>
    <td mat-cell *matCellDef="let element">{{element.Id}}</td>
  </ng-container>

  <ng-container matColumnDef="amount">
    <th mat-header-cell *matHeaderCellDef>Amount</th>
    <td mat-cell *matCellDef="let element">{{element.Amount}}</td>
  </ng-container>

  <ng-container matColumnDef="currency">
    <th mat-header-cell *matHeaderCellDef>Currency</th>
    <td mat-cell *matCellDef="let element">{{element.Currency}}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

If you integrate this into your project, it should assist you in achieving your goal.

Answer №2

To tackle this issue, it appears that the solution lies in the following steps:

TS

...
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
...

// Make sure to define your interface before declaring your Class, immediately after importing necessary modules
export interface OrderToProcess{
  Id: string,
  Amount: number,
  Currency: string
}

...
// When defining class attributes
dataSource$: Observable<OrderToProcess[]>;
...

constructor() {

    // Assign the result of getPaymentIntents to dataSource$, using rxjs operator pipe and map
    this.dataSource$ = this.getPaymentIntents()
    .pipe(
      map( (data:Payment[]) => {

        let ordersToProcess: OrderToProcess[] = data.map( payment => 
           {
              Id: payment.id,
              Amount: payment.amount,
              Currency: payment.currency
           }
        );

        return ordersToProcess;

      }) //end map rxjs
    ); //end pipe rxjs

}

Finally, in your HTML file, simply bind the dataSource$ observable to your material table like so ([dataSource]="dataSource$")

NOTE: There might be a way to simplify the code even further by trying the following approach:


 this.dataSource$ = this.getPaymentIntents()
    .pipe(
      map( (data:Payment[]) => data.map( payment => 
           {
              Id: payment.id,
              Amount: payment.amount,
              Currency: payment.currency
           }
    )));

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

As soon as I inserted the image, the text vanished into thin air

The phrase "welcome" is not displaying after I included the av tag <!DOCTYPE html> <html> <style> @font-face { font-family: OpenSans; src: url(OpenSans-Bold.ttf); } * { ...

Outputting PHP variables in JavaScript is a common practice used to pass

I am struggling to use a variable in my JavaScript code. I have attempted a few methods but none seem to work. The variable I need to include in the JavaScript is option-<?php echo $option['product_option_id']; ?> Below is my current Java ...

Create a JavaScript array containing all the elements from a MySQL table

I am attempting to store my mysql table data in a JavaScript array. My goal is to push each element of the table into the array so that it looks like this: array[0] = [question: information01, answer: information02...] array[1] = [question: information11, ...

Using CSS in combination with AngularJS, you can create a dynamic growth effect for a div element that initially

I am currently using AngularJS to dynamically add divs to a section. My goal is to have each div start with a static width and then grow dynamically as I continue to add more divs. How can I achieve this? The following code is not producing the desired re ...

Executing an SQL delete query with a button click using a JavaScript function in PHP

I have created a setup with three essential files - index.html, database.php, and function.js. In database.php, there is a form generated containing a delete button that triggers the deletion SQL query when clicked. The primary objective is to present a ta ...

What is the best way to incorporate websocket functionality for message push notifications with Django Rest Framework as the server-side backend and Angular 2

My goal is to incorporate websocket for sending push notifications to clients. I'm utilizing django-rest framework as the backend and angular2 as the frontend. I am aware that Django only supports the HTTP protocol, and I have been unabl ...

Steps for adding validation when clicking outside an input in an angular form:

I need help with implementing email validation in my Angular project. Here is the code snippet for the email input: https://i.sstatic.net/vipUc.png This is the code snippet: <form [formGroup]="emailGroupForm" (ngSubmit)="formSubmitted() ...

Having trouble seeing the output on the webpage after entering the information

I can't seem to figure out why the result is not displaying on the HTML page, so for now I have it set up as an alert. <h1>Factorial Problem</h1> <form name="frm1"> Enter any number :<input type="text" name="fact1"& ...

Error message: Impossibile to locate module 'formik' in 'C:Live projectsNew folder (2)src\_metronicpartialsmodalscreate-app'

"dependencies": { "@formatjs/intl-pluralrules": "^4.0.28", "@formatjs/intl-relativetimeformat": "^9.1.7", "@fortawesome/fontawesome-free": "^5.15.3", "@popperjs/core": "~2.10.1", "animate.css": "^4.1.1", "apexcharts": "^3.27.1", ...

Retrieve the href value from a DOM element

I'm currently working on a piece of code that aims to extract only the hrefs from the nav section. Here's what I have so far: componentDidMount(){ $("nav ul li a").each(function(k, j){ console.log(j); }); } So far, I've bee ...

Create a consistent number based on quantity

Looking to generate a sequence of numbers equal to the count in PHP or JavaScript for my application. For example, if my total count is 7: <?php $total = 7; I would like to generate seven 1's like this: $split_one = [1,1,1,1,1,1,1]; If my count ...

The function is returning an undefined value in node.js due to the boolean condition

I have two functions. The first one is in auth.js, and it looks like this: const adminCheck = (req, res) => { console.log(“one”) UtilRole.roleCheck(req, res, ‘ADMIN’, (response) => { if(response) { return true ...

Navigating the jQuery UI Accordion: selecting a panel via the top menu to reveal its contents while simultaneously closing any

Being a native French speaker, I ask for your understanding regarding any English errors made. Additionally, while I am comfortable with (x)HTML and CSS, I am a complete beginner in jQuery. My current project involves setting up a jQuery UI Accordion with ...

Handling ASP.net POST requests in Angular2

Here is a method called gotoDispatch: gotoDispatch() { // const headers = new Headers(); headers.append('Content-Type', 'application/json'); var url = 'Users/Cards'; var dto = { 'jsonSearchCards&apo ...

Error: The type '{ children: Element[]; className: string; }' cannot be assigned to the type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

My current setup involves using Next.js and TypeScript While working in Visual Studio Code, I encountered an error message stating Type error: Type '{ children: Element[]; className: string; }' is not assignable to type 'DetailedHTMLProps&l ...

Interfaces and Accessor Methods

Here is my code snippet: interface ICar { brand():string; brand(brand:string):void; } class Car implements ICar { private _brand: string; get brand():string { return this._brand; } set brand(brand:string) { this. ...

Error messages in login form using AJAX in Rails

My issue seems to be quite complicated, but let's simplify it. I am working with devise and I need to display error messages below the form on my login page. Currently, when a user enters the wrong password, I see an error POST 401 (Unauthorized) in t ...

Encountered a Next-Auth Error: Unable to fetch data, TypeError: fetch failed within

I've been struggling with a unique issue that I haven't found a solution for in any other forum. My Configuration NextJS: v13.0.3 NextAuth: v4.16.4 npm: v8.19.2 node: v18.12.1 When the Issue Arises This particular error only occurs in the pr ...

Encountering a 404 error while accessing my meticulously crafted express server

After ensuring that the server is correctly set up and without any errors related to imports or missing libraries, I utilized cors for development purposes. A 404 error persisted even after attempting to comment out the bodyparser. https://i.stack.imgur.c ...

When you invoke a function within Angular 1.5 as a fresh component emerges

I have a component where clicking on a button triggers the invocation of another component. model.alert = function () { modalInstance = $uibModal.open({ template: '<abc-xyz return-value="model" on-cancel="Cancel()">& ...