首页 > 解决方案 > 带有 CountryName 的 Angular CityList

问题描述

在我的I have cities withid andname andcounteyid and countries withid andname how to iterate thisresponse.value to get a list havingcityid,name,countryid`?

在此处输入图像描述

标签: angular12

解决方案


我猜你可以使用 map 函数,这里有一些你的代码示例。我像这样扩展您的列表,结果如下所示

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

推荐阅读