How to do ngFor loop on nested json object?

前端 未结 4 742
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 19:31

It seems to be simple thing to do, but somehow i did not get inner array element to do ngFor loop on Angular 2.

I have json array as below, and i need to iterate thr

相关标签:
4条回答
  • 2020-12-20 19:49

    You will want to convert your object to an iterable array.

      this.http.get('data.json')
                .subscribe((res) => {
                   let keyArr: any[] = Object.keys(res.json()[0].routes);
                   keyArr.forEach((key: any) => {
                      this.data.push(res.json()[0].routes[key]);
                   });
                });
    

    Here's a Plunker

    0 讨论(0)
  • 2020-12-20 19:50

    Your routes should be defined as an json array however you have done a workaround to make routes as an array where you have explicitly given indexs (0, 1 ..) which is not correct.

    Solution is attached below:

    JSON should look like below:

    [
      {
        "routes": [{
                    "budget": 326,
                    "toCity": "United States",
                    "QuoteIds": [
                        1,
                        2
                    ],
                    "options": 2
                }, {
                    "budget": 374,
                    "toCity": "Thailand",
                    "QuoteIds": [
                        3,
                        4
                    ],
                    "options": 2
                }
            ]
        }
    ]
    

    Way to assign it

    this.routes=resonseJson[0]['routes']
    

    Way to iterate it in html

    <div *ngFor="let route of routes">
        {{route.toCity}}
    </div>
    
    0 讨论(0)
  • 2020-12-20 19:50

    You have to iterate over object keys and not array elements. Hence either use

    Object.keys(responseJson[0].routes)
    

    that will return ["0","1", ..."178"]. Then use below:

    in component .ts :

    routes : any = responseJson[0].routes;
    

    in template :

    <div *ngFor="let key of Object.keys(routes)"> 
        {{routes.key.budget}}    
    </div>
    
    0 讨论(0)
  • 2020-12-20 19:56

    You have gotten some good answers here, but all are manipulating your response and changing the build of it, instead of treating it as is. There is some other data in your response and want to retain the data, so here's a solution using Pipe instead.

    You seem to have two objects in your array, but only one contains routes. Will this always be the case? If not, you might want to iterate the response and show all routes (if exists) for all objects, so I'd iterate the array first, and then iterate the routes:

    <!-- Iterate array -->
    <div *ngFor="let obj of jsonData">
      <!-- iterate routes for each object using pipe -->
      <div *ngFor="let route of obj.routes | keys">
        {{route.toCity}}
      </div>
    </div>
    

    And then the keys pipe:

    @Pipe({ name: 'keys',  pure: false })
    export class KeysPipe implements PipeTransform {
        transform(value: any, args?: any[]): any[] {
          // check if "routes" exists
          if(value) {
            // create instance vars to store keys and final output
            let keyArr: any[] = Object.keys(value),
                dataArr = [];
    
            // loop through the object,
            // pushing values to the return array
            keyArr.forEach((key: any) => {
                dataArr.push(value[key]);
            });
            // return the resulting array
            return dataArr;
          }
        }
    }
    

    This way you have not manipulated your response, and you have access to all other data that is coming with the response.

    Demo

    0 讨论(0)
提交回复
热议问题