2017-05-31 111 views
1

我在處理maps intScriptcript時遇到了一些麻煩。我所試圖做的是使用HashMap smilier於在爪哇發現例如,這裏是我的Java對象,使用Typescript中的地圖

public class Payment { 
    private String id; 
    private DateTime created; 

    //when they actually received the payment 
    private DateTime paidDate; 
    private String customerId; 
    private String companyId; 
    private BigDecimal amount; 
    private BigDecimal allocated; 
    private String description; 

    // it must be the Id of PaymentMethod 
    private String type; 

    //to improve it 
    private String currency; 

    private Boolean fullPaid = false; 
    private Boolean lockArchive = false; 

    //<InvoiceId, Amount> 
    private HashMap<String, BigDecimal> invoices = new HashMap<>(); 

    //getter and setters.... 

那麼我在打字稿做它來創建例如同級,

export class Payment { 
    public id?:string; 
    public created?:string; 
    public paid_date?:Date; 
    public customer_id?:string; 
    public company_id?:string; 
    public amount?:number; 
    public allocated?:number; 
    public description?:string; 
    public type?:string; 
    public currency?:string; 
    public full_paid?:boolean; 
    public lock_archive?:boolean; 
    public invoices: {[invoice_id:string]:number}; 


    constructor(id: string, created: string, paid_date: Date, customer_id: string, company_id: string, amount: number, allocated: number, description: string, type: string, currency: string, full_paid: boolean, lock_archive: boolean, invoices: { [p: string]: number }) { 
    this.id = id; 
    this.created = created; 
    this.paid_date = paid_date; 
    this.customer_id = customer_id; 
    this.company_id = company_id; 
    this.amount = amount; 
    this.allocated = allocated; 
    this.description = description; 
    this.type = type; 
    this.currency = currency; 
    this.full_paid = full_paid; 
    this.lock_archive = lock_archive; 
    this.invoices = invoices; 
    } 
} 

我想基本上添加到該invoices地圖,所以我有以下功能,

public invoicesMap = new Map<string, number>(); 

    constructor(public dialogRef: MdDialogRef<PaymentInvoiceSelectDialogComponent>, 
       private customerService:CustomerService, 
       private paymentServices: PaymentsService) { 

    } 

    ngOnInit() { 

    this.customer.subscribe((res)=>{ 
     this.customer_name = res.customer_name 
    }, error=>{ 
     console.error(<any>error); 
    }); 

    this.customerService.getListOfCustomerInvoices(this.customerId,'0','25') 
     .subscribe((res) =>{ 
     this.invoices = res; 
     }, 
     error => { 
     console.error(<any>error); 
     }); 

    } 

    selectedInvoice(invoiceId: string, amount: number, event:any) { 

    if(event.checked){ 

     if(!_.isEmpty(this.payment.invoices)){ 

     for(let [key, value] of this.invoicesMap) { 

      if (key !== invoiceId) { 

      this.invoicesMap.set(invoiceId, amount); 

      for(let [key, vvalue] of this.invoicesMap) { 

       if (key === invoiceId) { 

       this.availableAllocation = this.availableAllocation - vvalue; 

       } 
      } 
      } 
     } 
     } else { 

     this.invoicesMap.set(invoiceId,amount); 

     for(let [key, vvalue] of this.invoicesMap) { 
      if(key === invoiceId){ 
      this.availableAllocation = this.amount - vvalue; 
      } 
     } 
     } 
    } else if(!event.checked){ 

     for(let [key, vvalue] of this.invoicesMap) { 

     if (key === invoiceId) { 

      console.log("removing an item"); 
      this.availableAllocation += vvalue; 
     } 

     } 
    } 

    //at this point I would like to set this.payment.invoices to this.invoiceMap 
    for(let [key, value] of this.invoicesMap){ 
     let v = {invoice_id:key,amount:value}; 
     console.log(v); 
    } 

    this.payment.allocated = this.availableAllocation; 
    } 


    savePayment(){ 
    console.log(JSON.stringify(this.payment)); 
    // this.paymentServices.updatePayment(this.payment) 
    // .subscribe((res)=>{ 
    //  this.payment = res; 
    //  this.dialogRef.close(this.payment); 
    //  }, 
    //  error =>{ 
    //  console.log(<any>error); 
    //  }); 
    } 

的項目將被添加到invoiceMap但我現在遇到的問題是將invoiceMap添加到payment.invoices。如果有人能指出我的方向,這將是非常感激。由於

回答

1

您可以Payment將其更改爲Map還有:

public invoices: Map<string, number>; 

然後你就可以簡單地分配你有地圖。
或者你可以遍歷映射條目,把它們變成一個對象就像你在Payment

let m = new Map<string, number>(); 

let m2 = {} as { [key: string]: number }; 
m.forEach((value, key) => m2[key] = value); 
+0

我改變了發票付款地圖,就像你說的迭代地圖,並按預期工作,感謝您的幫助。 –

1

如果你是要去使用String作爲鍵沒有理由使用一個HashMap,在JavaScript中的每個對象,以及打字稿,是一個簡單的地圖。您需要使用Map類的唯一原因是您需要除String之外的其他內容作爲關鍵字。

請看看這個問題 - >How is a JavaScript hash map implemented?

+0

這讓我更加清楚,謝謝。 –

0

,如果我知道你想,你可以使用TypeLite什麼事:http://type.litesolutions.net/ 這個插件轉換任何類C#中的打字稿類。

在異步通信之前,您將請求解析爲json,並將其放入代碼中(或放入控制器中),然後將對象序列化爲json。

+0

謝謝,但我使用Java,另外我可以使用此https://github.com/vojtechhabarta/typescript-generator。 –