ng-repeat loop through object in object

angularjs, angularjs-ng-repeat, javascript, loops

Solution

Setting aside the missing braces in your object data, you could do something silly like

<tr ng-repeat="person in object">
    <td ng-repeat="keys in person">
        {{keys.name}}
    </td>   
</tr>

...which would do what you want (the inner ng-repeat will only loop one time, since each "person" only has one key ("person1", "person2"...) But a better solution is probably to change your data structure to either remove those unnecessary person1, person2, etc identifiers and treat it as an array:

$scope.object = [
    {name:'Joe'},
    {name:'Susan'}
];

or remove the array brackets and treat it as a hash table:

$scope.object = {
    person1: {name:'Bob'}, 
    person2: {name:'Ted'}
};

With either of those data structures, your HTML template would be the same:

<tr ng-repeat="person in object">
    <td>{{person.name}}</td>
</tr>

Right now you're trying to structure it as both an array and a hash, which gives no benefit and just makes accessing the data clumsier.

Problem

I'm confused how to loop through this data model. ``` $scope.object = [ {person1: {name: 'jon', height: 100}} , {person2: {name: 'joe', height: 200}}, {person3: {name: 'lisa', height: 150}}] ``` I'm trying ng-repeat like this ``` <tr ng-repeat = "person in object[0]"> <td>{{person.name}}</td> </tr> ``` This of course will only show 'jon'. How can I get all person(x).name? I could name them all person instead of person1, person2, but my data model for my project wont allow that. What can do? Thanks

Original source