IggyTheFool
IggyTheFool

Reputation: 29

How to display an object of objects in angular?

So, there is an object, inside which there are some other objects, and inside them there are more objects. Please, tell me how to output key values. I suppose *ngFor won't help

const data = {
    first_data: {
      id: "1",
      name: "First",
      type: "Administration",
      sections: {
        main_section: {
            id: "main_section",
            name: "Main Section",
            sub_sections: {
                sub_first: {
                    id: "12",
                    name:  "Subsection_name",
                    questions: {
                      first_question: {
                        id: "44",
                        name: "Answer"
                      },
                      second_question: {
                        id: "33",
                        name: "True"
                      }
                    }
                  }
                }
              }
            }
          }
        }

Upvotes: 2

Views: 1227

Answers (1)

pzaenger
pzaenger

Reputation: 11992

Use ngFor combined with KeyValuePipe:

Transforms Object or Map into an array of key value pairs.

<div *ngFor="let item of object | keyvalue">
  {{item.key}}:{{item.value}}
</div>

However, I guess you need some kind of custom pipe to transform your data. I found this thread including this answer - maybe worth a try.

Upvotes: 3

Related Questions