How to define Typescript Map of key value pair. where key is a number and value is an array of objects

后端 未结 3 2062
广开言路
广开言路 2021-01-30 20:41

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 imp

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 21:13

    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}));
            }
        });
    

提交回复
热议问题