GMan
GMan

Reputation: 119

Issue on complex JSON parsing (angularjs and javascript)

I have json as below,

{
    "natureitems": [
        {
            "groups": "fruits",
            "items": [
                {
                    "name": "apple",
                    "properties": {
                        "color": "red",
                        "shape": "round"
                    }
                },
                {
                    "name": "orange",
                    "properties": {
                        "color": "orange",
                        "shape": "round"
                    }
                }
            ]
        },
        {
            "groups": "vegitables",
            "items": [
                {
                    "name": "carrot",
                    "properties": {
                        "color": "red",
                        "shape": "cone"
                    }
                },
                {
                    "name": "tomoto",
                    "properties": {
                        "color": "red",
                        "shape": "round"
                    }
                }
            ]
        }
    ]
}

I need a output in single li ng-repeat(angular view) like

------------------
fruits(these titles row in different background colors)
------------------
apple   - round
orange  - round

----------------
vegitables
---------------
carrot - cone
tomoto - round

I have tried many for loops and angular foreach, but i am getting items separately and groups separately. But after that trying to bring the above structure (title-items-title -items) are very hard to bring in one array.

Is there any easy method for getting these items in this format into an array or object? so that i can easily bind to ng-repeat!

Upvotes: 0

Views: 111

Answers (1)

Max Novich
Max Novich

Reputation: 1169

If you are not bound to a single level, you need to use nested ng-repeat:

<ul>
    <li ng-repeat="group in object.natureitems">
        <span class="group-title">{{group.groups}}</span>
        <ul>
            <li ng-repeat="item in group.items">{{item.name+" - "+"item.properties.shape"}}</li>
        </ul>
    </li>
</ul>

here is an example plunker

Upvotes: 1

Related Questions