Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Typescript中使用映射_Javascript_Java_Angular_Typescript - Fatal编程技术网

Javascript 在Typescript中使用映射

Javascript 在Typescript中使用映射,javascript,java,angular,typescript,Javascript,Java,Angular,Typescript,我在使用maps int Typescript时遇到了一些问题。我想做的是使用一个HashMap smilier,它可以在Java中找到,例如这里是我的Java对象 public class Payment { private String id; private DateTime created; //when they actually received the payment private DateTime paidDate; private S

我在使用maps int Typescript时遇到了一些问题。我想做的是使用一个HashMap smilier,它可以在Java中找到,例如这里是我的Java对象

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....
我试图基本上添加到
发票
映射,因此我有以下功能

  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);
    //     });
  }
public invoicesMap=new Map();
构造函数(公共dialogRef:MdDialogRef,
私人客户服务:客户服务,
私人付费服务:付费服务){
}
恩戈尼尼特(){
this.customer.subscribe((res)=>{
this.customer\u name=res.customer\u name
},错误=>{
控制台错误(error);
});
this.customerService.getListOfCustomerInvoices(this.customerId,'0','25'))
.订阅((res)=>{
此参数=res;
},
错误=>{
控制台错误(error);
});
}
selectedInvoice(发票ID:字符串、金额:数字、事件:任意){
如果(已选中事件){
如果(!\ i空(此付款发票)){
for(让此.invoicesMap的[键,值]){
如果(键!==发票ID){
此.invoicesMap.set(invoiceId,amount);
for(让这个.invoicesMap的[key,vvalue]{
如果(键===发票ID){
this.availableAllocation=this.availableAllocation-v值;
}
}
}
}
}否则{
此.invoicesMap.set(invoiceId,amount);
for(让这个.invoicesMap的[key,vvalue]{
如果(键===发票ID){
this.availableAllocation=this.amount-v值;
}
}
}
}如果(!event.checked),则为else{
for(让这个.invoicesMap的[key,vvalue]{
如果(键===发票ID){
console.log(“删除项目”);
this.availableAllocation+=v值;
}
}
}
//此时,我想将this.payment.invoices设置为this.invoicesmap
for(让此.invoicesMap的[键,值]){
设v={invoice_id:key,amount:value};
控制台日志(v);
}
this.payment.allocated=this.availableAllocation;
}
储蓄支付(){
log(JSON.stringify(this.payment));
//this.paymentServices.updatePayment(this.payment)
//.订阅((res)=>{
//这个。支付=res;
//this.dialogRef.close(this.payment);
//     },
//错误=>{
//console.log(错误);
//     });
}

项目已添加到
invoiceMap
中,但我现在遇到的问题是将
invoiceMap
添加到
payment.invoices
。如果有人能给我指出正确的方向,我将不胜感激。谢谢

您也可以在
付款
中将其更改为
地图

public invoices: Map<string, number>;

如果要使用字符串作为键,就没有理由使用hashmap,javascript中的每个对象以及typescript都是一个简单的映射。需要使用Map类的唯一原因是,如果需要字符串以外的内容作为键


请看一下这个问题->

如果我了解您想要什么,您可以使用TypeLite: 此插件可转换typescript类中的任何类c


在异步通信之前,您将请求解析为json,并将其转换为代码(或控制器),然后使用序列化为json的对象进行响应。

最简单的方法是使用记录类型Record

const invoicesMap:记录
这将允许您拥有具有任何字符串和值的类型。只有在你事先不知道钥匙的情况下才使用它

您还可以查看部分类型。如果你有一些值,但不是全部


我在付款时将发票更改为map,正如您所说的,在地图上迭代并按预期工作,谢谢您的帮助。谢谢,但我使用的是Java,而且我可以使用这个。
public invoices: Map<string, number>;
let m = new Map<string, number>();

let m2 = {} as { [key: string]: number };
m.forEach((value, key) => m2[key] = value);
const invoicesMap : Record<string, any>