67

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?

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

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
usmanwalana
  • 1,002
  • 2
  • 14
  • 31

3 Answers3

101

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 tuple of 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;

(code in playground)

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • In your playground link there is an error in console Uncaught TypeError: Cannot read property 'push' of undefined(…) I implemented it using an interface and i got the same error. – usmanwalana Dec 05 '16 at 15:08
  • 1
    That's because I just copied your code, and in your code you are setting `myarray` to null and then try to push to it. So either don't set it to null, or just reassign a new array later – Nitzan Tomer Dec 05 '16 at 15:14
  • I am getting error on type keyword, using typescript 2.1.0 – d-man Jun 12 '17 at 15:05
  • @Faisalkhan That shouldn't happen. Type aliases were added in [typescript 1.4](https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#type-aliases). What's the error you're getting? – Nitzan Tomer Jun 12 '17 at 15:11
11

The most simple way is to use Record type Record<number, productDetails>

interface productDetails {
   productId : number , 
   price : number , 
   discount : number
};

const myVar : Record<number, productDetails> = {
   1: {
       productId : number , 
       price : number , 
       discount : number
   }
}
Yves M.
  • 29,855
  • 23
  • 108
  • 144
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
7

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] = [];
            }
            mappedItems[item.key].push({productId : item.productId , price : item.price , discount : item.discount}));
        });
Ryan M
  • 18,333
  • 31
  • 67
  • 74
kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25