Angular5:将嵌套对象转换为嵌套数组,以便在ngFor中使用

问题描述 投票:0回答:2

我有一个这样的嵌套对象:

data =   [
        {
            "id": "0001",
            "type": "donut",
            "name": "Cake",
            "ppu": 0.55,
            "batters":
                {
                    "batter":
                        [
                            { "id": "1001", "type": "Regular" },
                            { "id": "1002", "type": "Chocolate" },
                            { "id": "1003", "type": "Blueberry" },
                            { "id": "1004", "type": "Devil's Food" }
                        ]
                },
            "topping":
                [
                    { "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5007", "type": "Powdered Sugar" },
                    { "id": "5006", "type": "Chocolate with Sprinkles" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" }
                ]
        },
        {
            "id": "0002",
            "type": "donut",
            "name": "Raised",
            "ppu": 0.55,
            "batters":
                {
                    "batter":
                        [
                            { "id": "1001", "type": "Regular" }
                        ]
                },
            "topping":
                [
                    { "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" }
                ]
        },
        {
            "id": "0003",
            "type": "donut",
            "name": "Old Fashioned",
            "ppu": 0.55,
            "batters":
                {
                    "batter":
                        [
                            { "id": "1001", "type": "Regular" },
                            { "id": "1002", "type": "Chocolate" }
                        ]
                },
            "topping":
                [
                    { "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" }
                ]
        }
    ]

我想遍历这个对象并使用batter在我的html中显示*ngFor对象数据但是当我嵌套*ngFor以实现相同的这样:

 <div *ngFor="let item of data">
       <!--Display first level elements here-->
       <div *ngFor="let batter of item.batters">
         <!--Display secondlevel elements here-->
       </div>
    </div>

我收到以下错误说

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

如何将此嵌套对象转换为嵌套数组?或者有没有更好的方法来使用Typescript / EcmaScript / RxJS展平此对象?

angular typescript nest-nested-object
2个回答
1
投票

编辑之后:json结构看起来很奇怪。为了反复击球,我猜应该是这样的。

<div *ngFor="let item of data">
   <div *ngFor="let i of item.batters.batter">
      {{i.id}}, {{i.type}
   </div>
</div>

1
投票

它发生了,因为你试图在循环中迭代对象,而不是数组,看看这里:

"batters":{
      "batter":
          [
             { "id": "1001", "type": "Regular" }
           ]
},

这可以简化,我相信这种输出格式:

"batters":[
   { "id": "1001", "type": "Regular" },
   { "id": "1002", "type": "Regular" }
]

它看起来更好,它将适用于您的情况。

简单:

let part = "batters":
                {
                    "batter":
                        []}

let extract = part["betters"]["batter"];

mainOrray["betters"] = extract;
© www.soinside.com 2019 - 2024. All rights reserved.