【问题标题】:How to define Typescript Map of key value pair. where key is a number and value is an array of objects如何定义键值对的 Typescript Map。其中key是一个数字,value是一个对象数组
【发布时间】:2017-04-19 23:32:21
【问题描述】:

在我的 angular2 应用程序中,我想创建一个以数字为键并返回对象数组的映射。我目前正在以下列方式实施,但没有运气。我应该如何实现它或者我应该为此目的使用其他一些数据结构?我想使用地图,因为它可能很快?

声明

 private myarray : [{productId : number , price : number , discount : number}];

priceListMap : Map<number, [{productId : number , price : number , discount : number}]> 
= new Map<number, [{productId : number , price : number , discount : number}]>();

用法

this.myarray.push({productId : 1 , price : 100 , discount : 10});
this.myarray.push({productId : 2 , price : 200 , discount : 20});
this.myarray.push({productId : 3 , price : 300 , discount : 30});
this.priceListMap.set(1 , this.myarray);
this.myarray = null;

this.myarray.push({productId : 1 , price : 400 , discount : 10});
this.myarray.push({productId : 2 , price : 500 , discount : 20});
this.myarray.push({productId : 3 , price : 600 , discount : 30});
this.priceListMap.set(2 , this.myarray);
this.myarray = null;

this.myarray.push({productId : 1 , price : 700 , discount : 10});
this.myarray.push({productId : 2 , price : 800 , discount : 20});
this.myarray.push({productId : 3 , price : 900 , discount : 30});
this.priceListMap.set(3 , this.myarray);
this.myarray = null;

如果我使用this.priceList.get(1);,我想得到一个包含 3 个对象的数组

【问题讨论】:

    标签: arrays json angular typescript


    【解决方案1】:

    首先,为您的对象定义一个类型或接口,这将使事情更具可读性:

    type Product = { productId: number; price: number; discount: number };
    

    您使用大小为 1 的 a tuple 而不是数组,它应该如下所示:

    let myarray: Product[];
    let priceListMap : Map<number, Product[]> = new Map<number, Product[]>();
    

    所以现在可以正常工作了:

    myarray.push({productId : 1 , price : 100 , discount : 10});
    myarray.push({productId : 2 , price : 200 , discount : 20});
    myarray.push({productId : 3 , price : 300 , discount : 30});
    priceListMap.set(1 , this.myarray);
    myarray = null;
    

    (code in playground)

    【讨论】:

    • 在您的 Playground 链接中,控制台出现错误 Uncaught TypeError: Cannot read property 'push' of undefined(...) 我使用接口实现了它,但遇到了同样的错误。
    • 那是因为我刚刚复制了您的代码,并且在您的代码中您将myarray 设置为null,然后尝试推送到它。所以要么不要将其设置为null,要么稍后重新分配一个新数组
    • 我在 type 关键字上遇到错误,使用 typescript 2.1.0
    • @Faisalkhan 这不应该发生。在typescript 1.4 中添加了类型别名。你遇到了什么错误?
    【解决方案2】:

    您也可以完全跳过创建字典。我用下面的方法来解决同样的问题。

     mappedItems: {};
     items.forEach(item => {     
            if (mappedItems[item.key]) {
               mappedItems[item.key].push({productId : item.productId , price : item.price , discount : item.discount});
            } else {
              mappedItems[item.key] = [];
              mappedItems[item.key].push({productId : item.productId , price : item.price , discount : item.discount}));
            }
        });
    

    【讨论】:

      【解决方案3】:

      最简单的方法是使用Record输入Record&lt;number, productDetails&gt;

      interface productDetails {
         productId : number , 
         price : number , 
         discount : number
      };
      
      const myVar : Record<number, productDetails> = {
         1: {
             productId : number , 
             price : number , 
             discount : number
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-15
        • 2021-03-10
        • 2022-10-30
        • 1970-01-01
        • 2019-05-17
        • 2020-09-03
        • 1970-01-01
        相关资源
        最近更新 更多