Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping array of object values to Interface type in Typescript

I have an array called dealers in a JSON document that contains several object as shown below.

"dealers" : [ 
    {
        "name" : "BMW Dealer",
        "country" : "Belgium",
        "code" : "123"
    },
        {
        "name" : "Audi Dealer",
        "country" : "France",
        "code" : "124"
    },
        {
        "name" : "VW Dealer",
        "country" : "Germany",
        "code" : "125"
    }
]

I also have an interface type as shown below and a variable of this interface type.

interface IDealer extends IZone {
    dealerName: string;
    dealerCode: string;
    dealerCountry: string
}

var countryDealers IDealer;

I'd like to iterate through the dealers array of objects and populate the countryDealers variable.

How can I achieve this please?

like image 795
Ali Celebi Avatar asked Dec 07 '18 11:12

Ali Celebi


1 Answers

have you tried with the .map() function of ES6 ?

like:

    let myInterfacesArray = countryDealers.map(xx=>{

    return <IDealer>
    {
         dealerName : xx.name,
         dealerCode : xx.code,
         dealerCountry : xx.country
          // and so on
     };

  });

Hope it help you!!

like image 169
federico scamuzzi Avatar answered Oct 19 '22 11:10

federico scamuzzi