Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 661
usmanwalana Avatar asked Dec 05 '16 14:12

usmanwalana


People also ask

How do you define a key-value pair in TypeScript?

Use an index signature to define a key-value pair in TypeScript, e.g. const employee: { [key: string]: string | number } = {} . An index signature is used when we don't know all the names of a type's keys ahead of time, but we know the shape of their values.

How do you get a Keys map in TypeScript?

We use the get() method of a map in TypeScript to get the value for a key in a map. A map is a key-value pair data structure.

How do I create a map object in TypeScript?

Creating a MapUse Map type and new keyword to create a map in TypeScript. let myMap = new Map<string, number>(); To create a Map with initial key-value pairs, pass the key-value pairs as an array to the Map constructor.


3 Answers

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)

like image 151
Nitzan Tomer Avatar answered Oct 07 '22 15:10

Nitzan Tomer


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
   }
}
like image 9
aWebDeveloper Avatar answered Oct 07 '22 15:10

aWebDeveloper


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}));
        });
like image 7
kumar chandraketu Avatar answered Oct 07 '22 15:10

kumar chandraketu