18

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?

2
  • what countryDealers should have? Commented Dec 7, 2018 at 11:57
  • @Sajeetharan countryDealers will have all the properties from the interface and respective data retrieved from dealers array i.e. name, code and country. Commented Dec 7, 2018 at 11:59

2 Answers 2

46

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!!

Sign up to request clarification or add additional context in comments.

Comments

3

I could not use

return <IInterface>
{
  ...
}

in a tsx files because I started getting errors.

You will have to use

return {
  ...
} as IInterface;

instead

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.