0

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

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 展平这个对象?

4

2 回答 2

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;
于 2018-03-28T00:30:49.857 回答
0

编辑后: json 结构看起来很奇怪。为了迭代槽击球手,我猜应该是这样的。

<div *ngFor="let item of data">
   <div *ngFor="let i of item.batters.batter">
      {{i.id}}, {{i.type}
   </div>
</div>
于 2018-03-27T21:45:55.617 回答