I'm struggling a bit with working on maps in Typescript. My goal is to use a HashMap similar to what's available in Java. For example, here is an example of my Java object:
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....
In Typescript, I've attempted to create a similar class. Here's an example:
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;
}
}
I'm trying to add to the 'invoices' map. Here's the function I have:
public invoicesMap = new Map<string, number>();
constructor(public dialogRef: MdDialogRef<PaymentInvoiceSelectDialogComponent>,
private customerService:CustomerService,
private paymentServices: PaymentsService) {
}
ngOnInit() {
// Implementation details omitted for brevity.
}
selectedInvoice(invoiceId: string, amount: number, event:any) {
// Implementation details omitted for brevity.
}
savePayment(){
console.log(JSON.stringify(this.payment));
// Implementation details omitted for brevity.
}
The items are successfully added to the 'invoiceMap,' but now I am facing challenges adding 'invoiceMap' to 'payment.invoices.' Any guidance on how to proceed would be greatly appreciated. Thank you.