3

I have a JSON that has some nested objects in it. So to show them in view, I created this:

<div *ngFor="let item of trees">
    <div *ngFor="let obj of item | keyvalue">
        <div *ngFor="let obj1 of obj.value | keyvalue">
            <div *ngIf="!ifLeaf(obj1.value)">
                <b>{{ obj1.key }}</b>
                <div *ngFor="let obj2 of obj1.value | keyvalue">
                    <div *ngIf="ifLeaf(obj2.value)">
                        <i>{{ obj2.key }}</i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

And the JSON looks like that:

trees[
        header: {
            first: {
                title: {
                    name: "Test"
                }
            },
            second: {
                title: {
                    top: {
                        name: "Test"
                    }
                },
                desc: {
                    name: "Test"
                }
            }
        }
    ]

That code can show up to 2 nested objects with its keys and values. But what if the last object has 3 more nested objects within its own object? Is there a way that I can do that dynamically, without hardcoding? I'm sure there are better solutions for that. I'd be really thankful!

3
  • Try using some treeview package to which you pass nested object! Commented Nov 18, 2019 at 12:40
  • Can you show trees Commented Nov 18, 2019 at 12:41
  • maybe you should spread the object in TS file and use that in HTML. You can write a recursive function to spread the object. Since deepness of nested object is unknown only option could be a recursive function Commented Nov 18, 2019 at 12:42

1 Answer 1

1

You can solve it by creating a recursive component. For example https://netbasal.com/recursion-in-angular-components-1cd636269b12

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.