json 如何定义键值对的 Typescript Map。其中键是一个数字,值是一个对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40976536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define Typescript Map of key value pair. where key is a number and value is an array of objects
提问by usmanwalana
In my angular2 app i want to create a map which takes a number as key and returns an array of objects. I am currently implementing in following way but no luck. How should i implement it or should i use some other data structure for this purpose? I want to use map because maybe its fast?
在我的 angular2 应用程序中,我想创建一个以数字为键并返回一个对象数组的地图。我目前正在按以下方式实施,但没有运气。我应该如何实现它还是应该为此使用其他一些数据结构?我想使用地图,因为它可能很快?
Declaration
宣言
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}]>();
Usage
用法
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;
I want to get an array of 3 objects if i use this.priceList.get(1);
如果我使用,我想获得 3 个对象的数组 this.priceList.get(1);
回答by Nitzan Tomer
First thing, define a type or interface for your object, it will make things much more readable:
首先,为你的对象定义一个类型或接口,它会让事情变得更具可读性:
type Product = { productId: number; price: number; discount: number };
You used a tupleof size one instead of array, it should look like this:
let myarray: Product[];
let priceListMap : Map<number, Product[]> = new Map<number, Product[]>();
So now this works fine:
所以现在这工作正常:
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;
(操场上的代码)
回答by kumar chandraketu
you can also skip creating dictionary altogether. i used below approach to same problem .
您也可以完全跳过创建字典。我用下面的方法来解决同样的问题。
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}));
}
});

