Search code examples
angular12

Angular Access list Properties


In myI have cities withidandnameandcounteyidand countries withidandnamehow to iterate thisresponse.valueto get a list havingcityid,name,countryid`?

enter image description here


Solution

  • you can use map function I guess, here some example with your code. I extend your list like that and result shown below

           const value = [
                {
                    id: "1",
                    name: "cityname",
                    countryId: "countryId",
                    countries: [{ id: 1, name: "countryname" }],
                },
                {
                    id: "2",
                    name: "cityname-2",
                    countryId: "countryId-2",
                    countries: [{ id: 2, name: "countryname-2" }],
                },
                {
                    id: "3",
                    name: "cityname-3",
                    countryId: "countryId-3",
                    countries: [{ id: 3, name: "countryname-3" }],
                },
            ];
    
            // you can check your list result
            console.log(
                value.map((m) => ({
                    cityId: m.id,
                    cityName: m.name,
                    countryId: m.countryId,
                }))
            );