简体   繁体   English

如何在 TypeScript 中的对象数组中将数据从一个对象数组获取到另一个对象数组中

[英]How to Get Data From One Array of Objects into another in Array of Objects in TypeScript

I have a get request in a component that returns a response我在返回响应的组件中有一个获取请求

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

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

The response data looks something like this (the "Payment" type)响应数据看起来像这样(“付款”类型)

[
    {
        "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 want this data to be in a "Material Table" https://material.angular.io/components/table/overview我希望这些数据位于“材料表” https://material.angular.io/components/table/overview

But I only want it to display a subset of the data.但我只希望它显示数据的一个子集。 (In the future I will want to combine another response's data into the table as well) (将来我也想将另一个响应的数据合并到表中)

The type I want to pass to the table as a dataSource is this我想作为数据源传递给表的dataSource是这个

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

How do I go about converting one type into the other, I've tried filter() map() object.entries() and I'm sure I'm not using them right but none seem to do what I am after.我如何 go 关于将一种类型转换为另一种类型,我试过filter() map() object.entries()并且我确定我没有正确使用它们,但似乎没有一个能满足我的要求。

Thanks for any help!谢谢你的帮助!

You are indeed looking for map .您确实在寻找map Assuming we have the following function to mock your HTTP call:假设我们有以下 function 来模拟您的 HTTP 调用:

  // Imports needed for rest of example code
  import { map, Observable, of } from 'rxjs';

  /**
   * getPaymentIntents mocks HTTP call with 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',
      },
    ]);
  }
}

You can map to your type in your component using the following (or something similar:您可以 map 使用以下(或类似的东西)在您的组件中输入您的类型:

  // Table columns
  displayedColumns: string[] = ['id', 'amount', 'currency'];
  // Our table data source
  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;
      })
    );
  }

And to display them in your table:并将它们显示在您的表格中:

<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 paste this in to your project I believe it should help you accomplish what you're aiming for.如果您将其粘贴到您的项目中,我相信它应该可以帮助您完成您的目标。

I guess that you just need to do this:我想你只需要这样做:

TS TS

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

// Declare your interface before declaring your Class, right after your imports
export interface OrderToProcess{
  Id: string,
  Amount: number,
  Currency: string
}

...
// In your class attributes declaration
dataSource$: Observable<OrderToProcess[]>;
...

constructor() {

    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

}

Then, in your HTML just pass the dataSource$ observable to your material table as usual ([dataSource]="dataSource$")然后,在您的 HTML 中,只需像往常一样将可观察到的 dataSource$ 传递给您的材料表([dataSource]="dataSource$")

NOTE: I'm not completely sure, but maybe you can even simplify it further just by doing the following:注意:我不完全确定,但也许您甚至可以通过执行以下操作进一步简化它:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM